View Javadoc
1   package net.sf.environments;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Arrays;
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.plugin.MojoFailureException;
8   import org.apache.maven.plugins.annotations.Component;
9   import org.apache.maven.plugins.annotations.LifecyclePhase;
10  import org.apache.maven.plugins.annotations.Mojo;
11  import org.apache.maven.plugins.annotations.Parameter;
12  import org.codehaus.plexus.archiver.Archiver;
13  import org.codehaus.plexus.archiver.jar.JarArchiver;
14  import org.codehaus.plexus.archiver.manager.ArchiverManager;
15  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
16  import org.codehaus.plexus.util.StringUtils;
17  
18  /**
19   * This goal will create separate packages out of the given environment
20   * directory.
21   *
22   * @author Karl-Heinz Marbaise
23   * <a href="mailto:khmarbaise@soebes.de">khmarbaise@soebes.de</a>
24   */
25  @Mojo(name = "configuration", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true)
26  public class ConfigurationMojo extends AbstractEnvironmentsMojo {
27  
28  	/**
29  	 * The JAR archiver needed for archiving the environments.
30  	 */
31  	@Component(role = Archiver.class, hint = "jar")
32  	private JarArchiver jarArchiver;
33  
34  	@Component
35  	private ArchiverManager manager;
36  
37  	/**
38  	 * The kind of archive we should produce {@code zip}, {code jar} etc.
39  	 */
40  	@Parameter(defaultValue = "jar")
41  	private String archiveType;
42  
43  	@Override
44  	public void execute() throws MojoExecutionException, MojoFailureException {
45  		String[] identifiedEnvironments = getTheEnvironments(getSourceDirectory());
46  
47  		if (identifiedEnvironments.length == 0) {
48  			getLog().warn("No Environment directories found.");
49  			return;
50  		}
51  
52  		validateEnvironments(identifiedEnvironments);
53  
54  		createLoggingOutput(identifiedEnvironments);
55  
56  		File resourceResult = createPluginResourceOutput();
57  
58  		String memEnv = getMavenSession().getUserProperties().getProperty("em.env", null);
59  
60  		if (StringUtils.isNotBlank(memEnv)) {
61  			if (Arrays.asList(identifiedEnvironments).contains(memEnv)) {
62  				processEnvironment(memEnv, resourceResult);
63  			} // Environment doesn't exist message is already logged inside AbstractEnvironmentMojo.filterResourcesToTarget. Not logging here to avoid duplication
64  		} else {
65  			for (String environment : identifiedEnvironments) {
66  				processEnvironment(environment, resourceResult);
67  			}
68  		}
69  
70  		filterResourcesToTarget(identifiedEnvironments);
71                  filterResourcesToAdditionalDirectories(identifiedEnvironments);
72  	}
73  	
74  	protected void processEnvironment(final String environment, final File resourceResult) throws MojoExecutionException {
75  		// Check why this can happen?
76  		if (environment.isEmpty()) {
77  			getLog().warn("The given directory '" + environment + "' is empty.");
78  			return;
79  		}
80  
81  		if (shouldSkip(environment)) {
82  			return;
83  		}
84  
85  		filterResources(resourceResult, environment, false, false);
86  
87  		try {
88  			File targetDirectory = new File(resourceResult, environment);
89  			File createArchiveFile = createArchiveFile(null, targetDirectory, environment, archiveType);
90  			getProjectHelper().attachArtifact(getMavenProject(), archiveType, environment,
91  					createArchiveFile);
92  		} catch (NoSuchArchiverException e) {
93  			getLog().error("Archive creation failed.", e);
94  		} catch (IOException e) {
95  			getLog().error("IO Exception.", e);
96  		}
97  	}
98  
99  	@Override
100 	protected JarArchiver getJarArchiver() {
101 		return jarArchiver;
102 	}
103 }