2121import java .io .File ;
2222import java .util .ArrayList ;
2323import java .util .Arrays ;
24+ import java .util .Collections ;
25+ import java .util .HashSet ;
26+ import java .util .Iterator ;
2427import java .util .List ;
28+ import java .util .Set ;
2529
26- import org .apache .maven .artifact .DependencyResolutionRequiredException ;
30+ import org .apache .maven .artifact .Artifact ;
31+ import org .apache .maven .artifact .factory .ArtifactFactory ;
32+ import org .apache .maven .artifact .metadata .ArtifactMetadataSource ;
33+ import org .apache .maven .artifact .repository .ArtifactRepository ;
34+ import org .apache .maven .artifact .resolver .ArtifactCollector ;
35+ import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
36+ import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
37+ import org .apache .maven .artifact .resolver .ArtifactResolver ;
38+ import org .apache .maven .artifact .resolver .DebugResolutionListener ;
39+ import org .apache .maven .artifact .resolver .filter .ArtifactFilter ;
2740import org .apache .maven .execution .MavenSession ;
2841import org .apache .maven .plugin .MojoExecutionException ;
2942import org .apache .maven .plugin .MojoFailureException ;
3043import org .apache .maven .plugin .logging .Log ;
3144import org .apache .maven .project .MavenProject ;
45+ import org .apache .maven .project .MavenProjectBuilder ;
46+ import org .apache .maven .project .ProjectBuildingException ;
47+ import org .apache .maven .project .artifact .InvalidDependencyVersionException ;
3248import org .apache .maven .toolchain .Toolchain ;
3349import org .apache .maven .toolchain .ToolchainManager ;
50+ import org .codehaus .plexus .logging .LogEnabled ;
51+ import org .codehaus .plexus .logging .Logger ;
3452import org .codehaus .plexus .util .StringUtils ;
3553
36- public abstract class AbstractStartProcessMojo extends AbstractServerMojo {
54+ public abstract class AbstractStartProcessMojo extends AbstractServerMojo implements LogEnabled {
3755 /**
3856 * The maven project.
3957 *
@@ -52,6 +70,49 @@ public abstract class AbstractStartProcessMojo extends AbstractServerMojo {
5270 */
5371 private MavenSession session ;
5472
73+ /**
74+ * @component
75+ */
76+ private MavenProjectBuilder projectBuilder ;
77+
78+ /**
79+ * Local maven repository.
80+ *
81+ * @parameter expression="${localRepository}"
82+ * @required
83+ * @readonly
84+ */
85+ private ArtifactRepository localRepository ;
86+
87+ /**
88+ * Remote repositories.
89+ *
90+ * @parameter expression="${project.remoteArtifactRepositories}"
91+ * @required
92+ * @readonly
93+ */
94+ private List remoteArtifactRepositories ;
95+
96+ /**
97+ * @component
98+ */
99+ protected ArtifactFactory artifactFactory ;
100+
101+ /**
102+ * @component
103+ */
104+ private ArtifactResolver artifactResolver ;
105+
106+ /**
107+ * @component
108+ */
109+ private ArtifactCollector artifactCollector ;
110+
111+ /**
112+ * @component
113+ */
114+ private ArtifactMetadataSource artifactMetadataSource ;
115+
55116 /**
56117 * @component
57118 */
@@ -98,11 +159,71 @@ public abstract class AbstractStartProcessMojo extends AbstractServerMojo {
98159 */
99160 private String argLine ;
100161
162+ private Logger logger ;
163+
164+ public void enableLogging (Logger logger ) {
165+ this .logger = logger ;
166+ }
167+
101168 protected boolean isDebug () {
102169 return debug ;
103170 }
104171
105- protected void startJavaProcess (String description , String mainClass , String [] args , File workDir , ProcessControl processControl ) throws MojoExecutionException , MojoFailureException {
172+ private List /*<File>*/ buildClasspath (Set /*<Artifact>*/ additionalArtifacts ) throws ProjectBuildingException , InvalidDependencyVersionException , ArtifactResolutionException , ArtifactNotFoundException {
173+ final Log log = getLog ();
174+
175+ // We need dependencies in scope test. Since this is the largest scope, we don't need
176+ // to do any additional filtering based on dependency scope.
177+ Set projectDependencies = project .getArtifacts ();
178+
179+ final Set artifacts = new HashSet (projectDependencies );
180+
181+ if (additionalArtifacts != null ) {
182+ for (Iterator it = additionalArtifacts .iterator (); it .hasNext (); ) {
183+ Artifact a = (Artifact )it .next ();
184+ if (log .isDebugEnabled ()) {
185+ log .debug ("Resolving artifact to be added to classpath: " + a );
186+ }
187+ ArtifactFilter filter = new ArtifactFilter () {
188+ public boolean include (Artifact artifact ) {
189+ String id = artifact .getDependencyConflictId ();
190+ for (Iterator it = artifacts .iterator (); it .hasNext (); ) {
191+ if (id .equals (((Artifact )it .next ()).getDependencyConflictId ())) {
192+ return false ;
193+ }
194+ }
195+ return true ;
196+ }
197+ };
198+ MavenProject p = projectBuilder .buildFromRepository (a , remoteArtifactRepositories , localRepository );
199+ if (filter .include (p .getArtifact ())) {
200+ Set s = p .createArtifacts (artifactFactory , Artifact .SCOPE_RUNTIME , filter );
201+ artifacts .addAll (artifactCollector .collect (s ,
202+ p .getArtifact (), p .getManagedVersionMap (),
203+ localRepository , remoteArtifactRepositories , artifactMetadataSource , filter ,
204+ Collections .singletonList (new DebugResolutionListener (logger ))).getArtifacts ());
205+ artifacts .add (p .getArtifact ());
206+ }
207+ }
208+ }
209+
210+ List /*<File>*/ cp = new ArrayList ();
211+ cp .add (project .getBuild ().getTestOutputDirectory ());
212+ cp .add (project .getBuild ().getOutputDirectory ());
213+ for (Iterator it = artifacts .iterator (); it .hasNext (); ) {
214+ Artifact a = (Artifact )it .next ();
215+ if (a .getArtifactHandler ().isAddedToClasspath ()) {
216+ if (a .getFile () == null ) {
217+ artifactResolver .resolve (a , remoteArtifactRepositories , localRepository );
218+ }
219+ cp .add (a .getFile ());
220+ }
221+ }
222+
223+ return cp ;
224+ }
225+
226+ protected final void startJavaProcess (String description , String mainClass , Set additionalDependencies , String [] args , File workDir , ProcessControl processControl ) throws MojoExecutionException , MojoFailureException {
106227 Log log = getLog ();
107228
108229 // Locate java executable to use
@@ -120,9 +241,9 @@ protected void startJavaProcess(String description, String mainClass, String[] a
120241 // Get class path
121242 List classpath ;
122243 try {
123- classpath = project . getTestClasspathElements ( );
124- } catch (DependencyResolutionRequiredException ex ) {
125- throw new MojoExecutionException ("Unexpected exception " , ex );
244+ classpath = buildClasspath ( additionalDependencies );
245+ } catch (Exception ex ) {
246+ throw new MojoExecutionException ("Failed to build classpath " , ex );
126247 }
127248 if (log .isDebugEnabled ()) {
128249 log .debug ("Class path elements: " + classpath );
0 commit comments