77import java .nio .file .FileVisitResult ;
88import java .nio .file .Files ;
99import java .nio .file .Path ;
10+ import java .nio .file .Paths ;
1011import java .nio .file .SimpleFileVisitor ;
1112import java .nio .file .StandardCopyOption ;
1213import java .nio .file .attribute .BasicFileAttributes ;
3334import io .github .project .classport .commons .ClassportInfo ;
3435import io .github .project .classport .commons .Utility ;
3536
36- @ Mojo (name = "embed" , defaultPhase = LifecyclePhase .PROCESS_CLASSES , requiresDependencyResolution = ResolutionScope .COMPILE_PLUS_RUNTIME )
37+ @ Mojo (name = "embed" , defaultPhase = LifecyclePhase .COMPILE , requiresDependencyResolution = ResolutionScope .COMPILE_PLUS_RUNTIME )
3738public class EmbeddingMojo
3839 extends AbstractMojo {
3940 /**
@@ -121,10 +122,32 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
121122 }
122123
123124 /**
124- * Embed metadata directly into an artifact JAR file in-place.
125- * This modifies the artifact in the local Maven repository.
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.
126128 */
127- private void embedArtifactInPlace (Artifact artifact )
129+ private File getAggregatedRepoRoot () {
130+ File topLevelBaseDir = session .getTopLevelProject ().getBasedir ();
131+ return new File (topLevelBaseDir , "classport-files" );
132+ }
133+
134+ /**
135+ * Destination path (jar) inside the aggregated repository for the given
136+ * artifact.
137+ */
138+ private File getRepoPathForArtifact (Artifact artifact , File repoRoot ) {
139+ String groupPath = artifact .getGroupId ().replace ('.' , File .separatorChar );
140+ File baseDir = Paths .get (repoRoot .getAbsolutePath (), groupPath , artifact .getArtifactId (), artifact .getVersion ()).toFile ();
141+ String classifierPart = artifact .getClassifier () != null ? "-" + artifact .getClassifier () : "" ;
142+ String extension = artifact .getArtifactHandler ().getExtension ();
143+ return Paths .get (baseDir .getAbsolutePath (), artifact .getArtifactId () + "-" + artifact .getVersion () + classifierPart + "." + extension ).toFile ();
144+ }
145+
146+ /**
147+ * Copy an artifact into the aggregated repository and embed metadata into the
148+ * copy, leaving the original (e.g. ~/.m2) untouched.
149+ */
150+ private void embedArtifactIntoRepo (Artifact artifact , File repoRoot )
128151 throws IOException , MojoExecutionException {
129152 File artifactFile = artifact .getFile ();
130153 if (artifactFile == null || !artifactFile .exists ()) {
@@ -138,21 +161,30 @@ private void embedArtifactInPlace(Artifact artifact)
138161 return ;
139162 }
140163
164+ File destJar = getRepoPathForArtifact (artifact , repoRoot );
165+ destJar .getParentFile ().mkdirs ();
166+
167+ Files .copy (artifactFile .toPath (), destJar .toPath (), StandardCopyOption .REPLACE_EXISTING );
168+
141169 ClassportInfo meta = getMetadata (artifact );
142- File tempJar = new File (artifactFile .getParent (), artifactFile .getName () + ".tmp" );
143- JarHelper pkgr = new JarHelper (artifactFile , tempJar , true );
170+ File tempJar = new File (destJar .getParent (), destJar .getName () + ".tmp" );
171+ JarHelper pkgr = new JarHelper (destJar , tempJar , true );
144172 pkgr .embed (meta );
145173
146- Files .delete (artifactFile .toPath ());
147- Files .move (tempJar .toPath (), artifactFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
174+ Files .delete (destJar .toPath ());
175+ Files .move (tempJar .toPath (), destJar .toPath (), StandardCopyOption .REPLACE_EXISTING );
148176
149- getLog ().info ("Embedded metadata into artifact : " + artifactFile .getAbsolutePath ());
177+ getLog ().info ("Embedded artifact into aggregated repo : " + destJar .getAbsolutePath ());
150178 }
151179
152180 @ Override
153181 public void execute () throws MojoExecutionException , MojoFailureException {
154182 Set <Artifact > dependencyArtifacts = project .getArtifacts ();
155183
184+ // Shared repository for all modules
185+ File aggregatedRepoRoot = getAggregatedRepoRoot ();
186+ aggregatedRepoRoot .mkdirs ();
187+
156188 getLog ().info ("Embedding metadata into compiled classes for module: " + project .getArtifactId ());
157189 try {
158190 embedDirectory (project .getArtifact (), classesDirectory );
@@ -165,7 +197,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
165197 getLog ().info ("Processing dependencies" );
166198 for (Artifact artifact : dependencyArtifacts ) {
167199 try {
168- embedArtifactInPlace (artifact );
200+ embedArtifactIntoRepo (artifact , aggregatedRepoRoot );
169201 } catch (IOException e ) {
170202 getLog ().error ("Failed to embed metadata for " + artifact + ": " + e );
171203 }
0 commit comments