3434import io .github .project .classport .commons .ClassportInfo ;
3535import io .github .project .classport .commons .Utility ;
3636
37- @ Mojo (name = "embed" , defaultPhase = LifecyclePhase .COMPILE , requiresDependencyResolution = ResolutionScope .COMPILE_PLUS_RUNTIME )
37+ @ Mojo (name = "embed" , defaultPhase = LifecyclePhase .PROCESS_CLASSES , requiresDependencyResolution = ResolutionScope .COMPILE_PLUS_RUNTIME )
3838public class EmbeddingMojo
3939 extends AbstractMojo {
4040 /**
@@ -121,16 +121,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
121121 });
122122 }
123123
124- /**
125- * Root directory (shared by all modules) where embedded artefacts are written
126- * in Maven-repository layout.
127- * We keep the name "classport-files" to maintain backward compatibility with the previous version of the plugin.
128- */
129- private File getAggregatedRepoRoot () {
130- File topLevelBaseDir = session .getTopLevelProject ().getBasedir ();
131- return new File (topLevelBaseDir , "classport-files" );
132- }
133-
134124 /**
135125 * Destination path (jar) inside the aggregated repository for the given
136126 * artifact.
@@ -165,6 +155,7 @@ private void embedArtifactIntoRepo(Artifact artifact, File repoRoot)
165155 destJar .getParentFile ().mkdirs ();
166156
167157 Files .copy (artifactFile .toPath (), destJar .toPath (), StandardCopyOption .REPLACE_EXISTING );
158+ getLog ().info (String .format ("Copied artifact %s to %s" , artifactFile .getAbsolutePath (), destJar .getAbsolutePath ()));
168159
169160 ClassportInfo meta = getMetadata (artifact );
170161 File tempJar = new File (destJar .getParent (), destJar .getName () + ".tmp" );
@@ -174,17 +165,17 @@ private void embedArtifactIntoRepo(Artifact artifact, File repoRoot)
174165 Files .delete (destJar .toPath ());
175166 Files .move (tempJar .toPath (), destJar .toPath (), StandardCopyOption .REPLACE_EXISTING );
176167
177- getLog ().info ("Embedded artifact into aggregated repo: " + destJar .getAbsolutePath ());
168+ getLog ().info ("Embedded metadata into: " + destJar .getAbsolutePath ());
169+
170+ // This makes Maven packaging plugins use the embedded JAR
171+ artifact .setFile (destJar );
172+ getLog ().debug ("Updated artifact reference: " + artifact .getId () + " -> " + destJar .getAbsolutePath ());
178173 }
179174
180175 @ Override
181176 public void execute () throws MojoExecutionException , MojoFailureException {
182177 Set <Artifact > dependencyArtifacts = project .getArtifacts ();
183178
184- // Shared repository for all modules
185- File aggregatedRepoRoot = getAggregatedRepoRoot ();
186- aggregatedRepoRoot .mkdirs ();
187-
188179 getLog ().info ("Embedding metadata into compiled classes for module: " + project .getArtifactId ());
189180 try {
190181 embedDirectory (project .getArtifact (), classesDirectory );
@@ -197,11 +188,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
197188 getLog ().info ("Processing dependencies" );
198189 for (Artifact artifact : dependencyArtifacts ) {
199190 try {
200- embedArtifactIntoRepo (artifact , aggregatedRepoRoot );
191+ File classportFilesDir = new File (project .getBuild ().getDirectory (), "classport-files" );
192+ classportFilesDir .mkdirs ();
193+ embedArtifactIntoRepo (artifact , classportFilesDir );
201194 } catch (IOException e ) {
202195 getLog ().error ("Failed to embed metadata for " + artifact + ": " + e );
203196 }
204-
205197 }
206198 }
207199
@@ -215,7 +207,7 @@ private ClassportInfo getMetadata(Artifact artifact) throws IOException, MojoExe
215207 .collect (Collectors .toList ()).contains (aId );
216208
217209 return new ClassportHelper ().getInstance (
218- getArtifactLongId (project .getArtifact ()), // TODO: Make into a constant
210+ getArtifactLongId (project .getArtifact ()),
219211 isDirectDependency ,
220212 aId ,
221213 artifact .getArtifactId (),
0 commit comments