2222package org .jboss .as .patch .generator .maven .plugin ;
2323
2424import java .io .File ;
25+ import java .io .IOException ;
2526import java .util .ArrayList ;
2627import java .util .List ;
2728
29+ import org .apache .maven .artifact .Artifact ;
2830import org .apache .maven .plugin .AbstractMojo ;
2931import org .apache .maven .plugin .MojoExecutionException ;
3032import org .apache .maven .plugins .annotations .LifecyclePhase ;
6870 *
6971 * @author Gunnar Morling
7072 */
71- @ Mojo ( name = "GenPatch " , defaultPhase = LifecyclePhase .GENERATE_RESOURCES )
73+ @ Mojo ( name = "generate-patch " , defaultPhase = LifecyclePhase .GENERATE_RESOURCES )
7274public class PatchGenMojo extends AbstractMojo {
7375
76+ private static final String LOG_FILE = "patchgen.log" ;
77+
7478 @ Parameter ( property = "patchConfig" , required = true )
7579 private File patchConfig ;
7680
@@ -98,9 +102,21 @@ public class PatchGenMojo extends AbstractMojo {
98102 @ Parameter ( property = "combineWith" )
99103 private File combineWith ;
100104
105+ @ Parameter ( property = "project.build.directory" )
106+ private File buildDirectory ;
107+
108+ @ Parameter ( property = "plugin.artifacts" )
109+ protected List <Artifact > pluginArtifacts ;
110+
101111 @ Override
102112 public void execute () throws MojoExecutionException {
103113 List <String > args = new ArrayList <>();
114+
115+ args .add ( "java" );
116+ args .add ( "-cp" );
117+ args .add ( getClasspath () );
118+ args .add ( PatchGenerator .class .getName () );
119+
104120 args .add ( PatchGenerator .APPLIES_TO_DIST + "=" + appliesToDist .getPath () );
105121 args .add ( PatchGenerator .OUTPUT_FILE + "=" + outputFile .getPath () );
106122 args .add ( PatchGenerator .PATCH_CONFIG + "=" + patchConfig .getPath () );
@@ -126,6 +142,43 @@ public void execute() throws MojoExecutionException {
126142 args .add ( PatchGenerator .COMBINE_WITH + "=" + combineWith .getPath () );
127143 }
128144
129- PatchGenerator .main ( args .toArray ( new String [0 ] ) );
145+ // Ideally, we'd just invoke PatchGenerator directly; currently we cannot do so due to https://issues.jboss.org/browse/MODULES-136:
146+ // JBoss Modules, when used as a library, will set some system properties to values causing trouble for other plug-ins later in the
147+ // build; e.g. SAXParserFactory is redirected to a JBoss Modules specific variant which then cannot be found by other users such as
148+ // the Checkstyle plug-in (which naturally doesn't have JBoss Modules on the plug-in dependency path). Hence we start patch-gen in
149+ // a separate process
150+ //
151+ // PatchGenerator.main( args.toArray( new String[0] ) );
152+ try {
153+ Process p = new ProcessBuilder ( args )
154+ .redirectOutput ( new File ( buildDirectory , LOG_FILE ) )
155+ .redirectError ( new File ( buildDirectory , LOG_FILE ) )
156+ .start ();
157+ p .waitFor ();
158+ }
159+ catch (IOException | InterruptedException e ) {
160+ throw new MojoExecutionException ( "Execution of PatchGenerator failed. See " + LOG_FILE + " for details." , e );
161+ }
162+
163+ if ( !outputFile .exists () ) {
164+ throw new MojoExecutionException ( "Execution of PatchGenerator failed. See " + LOG_FILE + " for details." );
165+ }
166+ }
167+
168+ private String getClasspath () {
169+ StringBuilder sb = new StringBuilder ();
170+ boolean first = true ;
171+
172+ for ( Artifact artifact : pluginArtifacts ) {
173+ if ( first ) {
174+ first = false ;
175+ }
176+ else {
177+ sb .append ( File .pathSeparator );
178+ }
179+ sb .append ( artifact .getFile ().getPath () );
180+ }
181+
182+ return sb .toString ();
130183 }
131184}
0 commit comments