1 package com.soebes.maven.plugins.iterator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.plugin.BuildPluginManager;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugin.descriptor.PluginDescriptor;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.shared.invoker.InvocationRequest;
35 import org.apache.maven.shared.invoker.InvocationResult;
36 import org.apache.maven.shared.invoker.Invoker;
37 import org.apache.maven.shared.invoker.MavenInvocationException;
38
39
40
41
42
43
44 @Mojo( name = "invoker", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true )
45 public class InvokerMojo
46 extends AbstractInvokerMojo
47 {
48
49
50
51
52 @Component
53 private BuildPluginManager pluginManager;
54
55 @Parameter( defaultValue = "${plugin}", required = true, readonly = true )
56 private PluginDescriptor pluginDescriptor;
57
58 @Parameter
59 private File workingDirectory;
60
61 @Component
62 private Invoker invoker;
63
64
65
66
67
68
69
70
71 public void execute()
72 throws MojoExecutionException, MojoFailureException
73 {
74 if ( isSkip() )
75 {
76 getLog().info( "Skip by user request." );
77 return;
78 }
79
80 if ( isNoneSet() )
81 {
82 throw new MojoExecutionException( "You have to use at least one. " + "Either items, "
83 + "itemsWithProperties, content or folder element!" );
84 }
85
86 if ( isMoreThanOneSet() )
87 {
88 throw new MojoExecutionException( "You can use only one element. "
89 + "Either items, itemsWithProperties, content or folder element but not more than one of them." );
90 }
91
92 File localRepository = new File( getMavenSession().getSettings().getLocalRepository() );
93
94 invoker.setLocalRepositoryDirectory( localRepository );
95
96
97
98
99
100
101
102
103 List<Exception> exceptions = new ArrayList<>();
104
105 for ( ItemWithProperties item : getItemsConverted() )
106 {
107 try
108 {
109 createLogOutput( item );
110 InvocationResult result = mavenCall( item );
111
112 if ( result.getExitCode() == 0 )
113 {
114 getLog().info( "------ Maven call was Ok." );
115 continue;
116 }
117
118 getLog().error( "------ Maven call was NOT Ok. for iteration " + item.getName() + " ( return code: " + result.getExitCode() + " )" );
119 if ( result.getExecutionException() != null )
120 {
121 getLog().error( result.getExecutionException().getMessage(),
122 result.getExecutionException().getCause() );
123 }
124
125 String failureMessage =
126 "Maven call failed with return code " + result.getExitCode() + " for iteration: " + item.getName();
127
128 if ( isFailAtEnd() )
129 {
130 exceptions.add( new RuntimeException( failureMessage ) );
131 }
132 else
133 {
134 throw new MojoFailureException( failureMessage );
135 }
136
137 }
138 catch ( MavenInvocationException e )
139 {
140
141 getLog().error( "------ ***** Command line options are wrong:", e );
142 throw new MojoExecutionException( "Command line options are wrong:", e );
143 }
144 }
145
146 if ( !exceptions.isEmpty() )
147 {
148 for ( Exception exception : exceptions )
149 {
150 getLog().error( exception );
151 }
152 throw new MojoExecutionException( "Failures during iterations." );
153 }
154 }
155
156 private File getWorkingDirectoryAfterPlaceHolderIsReplaced( ItemWithProperties currentValue )
157 {
158 File baseDir = getWorkingDirectory();
159 if ( baseDir != null && baseDir.toString().contains( getPlaceHolder() ) )
160 {
161 baseDir = new File( baseDir.toString().replaceAll( getPlaceHolder(), currentValue.getName() ) );
162 }
163 return baseDir;
164 }
165
166 private InvocationResult mavenCall( ItemWithProperties item )
167 throws MavenInvocationException
168 {
169 InvocationRequest request = createAndConfigureAnInvocationRequest( item );
170
171 OutputConsumer output = new OutputConsumer( getLog() );
172 request.setOutputHandler( output );
173
174 invoker.setWorkingDirectory( getWorkingDirectoryAfterPlaceHolderIsReplaced( item ) );
175
176 return invoker.execute( request );
177 }
178
179 private void createLogOutput( ItemWithProperties item )
180 {
181 StringBuilder sb = new StringBuilder( "------ " );
182 sb.append( " iterator-maven-plugin " );
183 sb.append( "( iteration: " );
184 sb.append( item.getName() );
185 sb.append( " )" );
186 getLog().info( sb.toString() );
187 }
188
189 public void setThreads( String threads )
190 {
191 this.threads = threads;
192 }
193
194 public File getWorkingDirectory()
195 {
196 return workingDirectory;
197 }
198
199 public void setWorkingDirectory( File workingDirectory )
200 {
201 this.workingDirectory = workingDirectory;
202 }
203
204 }