1 package com.soebes.maven.plugins.multienv;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.maven.archiver.MavenArchiver;
7 import org.apache.maven.artifact.DependencyResolutionRequiredException;
8 import org.apache.maven.plugin.MojoExecutionException;
9 import org.apache.maven.plugin.MojoFailureException;
10 import org.apache.maven.plugins.annotations.Component;
11 import org.apache.maven.plugins.annotations.LifecyclePhase;
12 import org.apache.maven.plugins.annotations.Mojo;
13 import org.apache.maven.plugins.annotations.Parameter;
14 import org.codehaus.plexus.archiver.Archiver;
15 import org.codehaus.plexus.archiver.ArchiverException;
16 import org.codehaus.plexus.archiver.jar.JarArchiver;
17 import org.codehaus.plexus.archiver.jar.ManifestException;
18 import org.codehaus.plexus.archiver.manager.ArchiverManager;
19 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
20 import org.codehaus.plexus.archiver.util.DefaultFileSet;
21
22
23
24
25
26
27 @Mojo( name = "configuration", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true )
28 public class ConfigurationMojo
29 extends AbstractMultiEnvMojo
30 {
31
32
33
34
35 @Component( role = Archiver.class, hint = "jar" )
36 private JarArchiver jarArchiver;
37
38 @Component
39 private ArchiverManager manager;
40
41
42
43
44 @Parameter( defaultValue = "jar" )
45 private String archiveType;
46
47 public void execute()
48 throws MojoExecutionException, MojoFailureException
49 {
50 String[] identifiedEnvironments = getTheEnvironments( getSourceDirectory() );
51
52 if ( identifiedEnvironments.length == 0 )
53 {
54 getLog().warn( "No Environment directories found." );
55 return;
56 }
57
58 validateEnvironments( identifiedEnvironments );
59
60 createLoggingOutput( identifiedEnvironments );
61
62 File resourceResult = createPluginResourceOutput();
63
64 filterResources( resourceResult );
65
66 for ( String environment : identifiedEnvironments )
67 {
68 getLog().info( "Building Environment: '" + environment + "'" );
69
70
71 if ( environment.isEmpty() )
72 {
73 getLog().warn( "The given directory '" + environment + "' is empty." );
74 continue;
75 }
76
77 try
78 {
79 File targetDirectory = new File( resourceResult, environment );
80 File createArchiveFile = createArchiveFile( targetDirectory, environment, archiveType );
81 getProjectHelper().attachArtifact( getMavenProject(), archiveType, environment,
82 createArchiveFile );
83 }
84 catch ( NoSuchArchiverException e )
85 {
86 getLog().error( "Archive creation failed.", e );
87 }
88 catch ( IOException e )
89 {
90 getLog().error( "IO Exception.", e );
91 }
92 }
93
94 }
95
96 private File createArchiveFile( File targetDirectory, String directory, String archiveExt )
97 throws NoSuchArchiverException, IOException, MojoExecutionException
98 {
99 final MavenArchiver mavenArchiver = new MavenArchiver();
100
101 mavenArchiver.setArchiver( jarArchiver );
102
103 jarArchiver.addFileSet( new DefaultFileSet( targetDirectory ) );
104
105
106 File resultArchive = getArchiveFile( getOutputDirectory(), getFinalName(), directory, archiveExt );
107
108 mavenArchiver.setOutputFile( resultArchive );
109 try
110 {
111 mavenArchiver.createArchive( getMavenSession(), getMavenProject(), getArchive() );
112 }
113 catch ( ArchiverException | ManifestException | DependencyResolutionRequiredException e )
114 {
115 getLog().error( e.getMessage(), e );
116 throw new MojoExecutionException( e.getMessage(), e );
117 }
118
119 return resultArchive;
120
121 }
122
123 }