diff --git a/maven-plugin/src/main/java/io/github/project/classport/plugin/EmbeddingMojo.java b/maven-plugin/src/main/java/io/github/project/classport/plugin/EmbeddingMojo.java index 970bc8e..467601c 100644 --- a/maven-plugin/src/main/java/io/github/project/classport/plugin/EmbeddingMojo.java +++ b/maven-plugin/src/main/java/io/github/project/classport/plugin/EmbeddingMojo.java @@ -34,7 +34,7 @@ import io.github.project.classport.commons.ClassportInfo; import io.github.project.classport.commons.Utility; -@Mojo(name = "embed", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) +@Mojo(name = "embed", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) public class EmbeddingMojo extends AbstractMojo { /** @@ -121,16 +121,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO }); } - /** - * Root directory (shared by all modules) where embedded artefacts are written - * in Maven-repository layout. - * We keep the name "classport-files" to maintain backward compatibility with the previous version of the plugin. - */ - private File getAggregatedRepoRoot() { - File topLevelBaseDir = session.getTopLevelProject().getBasedir(); - return new File(topLevelBaseDir, "classport-files"); - } - /** * Destination path (jar) inside the aggregated repository for the given * artifact. @@ -165,6 +155,7 @@ private void embedArtifactIntoRepo(Artifact artifact, File repoRoot) destJar.getParentFile().mkdirs(); Files.copy(artifactFile.toPath(), destJar.toPath(), StandardCopyOption.REPLACE_EXISTING); + getLog().info(String.format("Copied artifact %s to %s", artifactFile.getAbsolutePath(), destJar.getAbsolutePath())); ClassportInfo meta = getMetadata(artifact); File tempJar = new File(destJar.getParent(), destJar.getName() + ".tmp"); @@ -174,17 +165,17 @@ private void embedArtifactIntoRepo(Artifact artifact, File repoRoot) Files.delete(destJar.toPath()); Files.move(tempJar.toPath(), destJar.toPath(), StandardCopyOption.REPLACE_EXISTING); - getLog().info("Embedded artifact into aggregated repo: " + destJar.getAbsolutePath()); + getLog().info("Embedded metadata into: " + destJar.getAbsolutePath()); + + // This makes Maven packaging plugins use the embedded JAR + artifact.setFile(destJar); + getLog().debug("Updated artifact reference: " + artifact.getId() + " -> " + destJar.getAbsolutePath()); } @Override public void execute() throws MojoExecutionException, MojoFailureException { Set dependencyArtifacts = project.getArtifacts(); - // Shared repository for all modules - File aggregatedRepoRoot = getAggregatedRepoRoot(); - aggregatedRepoRoot.mkdirs(); - getLog().info("Embedding metadata into compiled classes for module: " + project.getArtifactId()); try { embedDirectory(project.getArtifact(), classesDirectory); @@ -197,11 +188,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Processing dependencies"); for (Artifact artifact : dependencyArtifacts) { try { - embedArtifactIntoRepo(artifact, aggregatedRepoRoot); + File classportFilesDir = new File(project.getBuild().getDirectory(), "classport-files"); + classportFilesDir.mkdirs(); + embedArtifactIntoRepo(artifact, classportFilesDir); } catch (IOException e) { getLog().error("Failed to embed metadata for " + artifact + ": " + e); } - } } @@ -215,7 +207,7 @@ private ClassportInfo getMetadata(Artifact artifact) throws IOException, MojoExe .collect(Collectors.toList()).contains(aId); return new ClassportHelper().getInstance( - getArtifactLongId(project.getArtifact()), // TODO: Make into a constant + getArtifactLongId(project.getArtifact()), isDirectDependency, aId, artifact.getArtifactId(), diff --git a/maven-plugin/src/test/java/io/github/project/classport/plugin/it/EmbeddingMojoIT.java b/maven-plugin/src/test/java/io/github/project/classport/plugin/it/EmbeddingMojoIT.java index 99d4ffa..02738e3 100644 --- a/maven-plugin/src/test/java/io/github/project/classport/plugin/it/EmbeddingMojoIT.java +++ b/maven-plugin/src/test/java/io/github/project/classport/plugin/it/EmbeddingMojoIT.java @@ -9,7 +9,6 @@ import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Disabled; import com.soebes.itf.jupiter.extension.MavenGoal; import com.soebes.itf.jupiter.extension.MavenJupiterExtension; @@ -20,7 +19,6 @@ import io.github.project.classport.analyser.CorrectnessAnalyser; import io.github.project.classport.commons.ClassportInfo; -@Disabled("Skipping this test until we find a way to embed using a single command") @MavenJupiterExtension public class EmbeddingMojoIT { @MavenGoal("package") @@ -44,6 +42,13 @@ void embed_dependency_simple_app(MavenExecutionResult result) { Set expectedIds = Set.of("org.apache.commons:commons-lang3:jar:3.17.0", "org.example:hello:jar:0.1.0"); Set actualIds = sbom.values().stream().map(ClassportInfo::id).collect(Collectors.toSet()); assertEquals(expectedIds, actualIds); + // Make sure that dependency in m2 repository is not embedded + Path m2Repository = Paths.get("target/maven-it/io/github/project/classport/plugin/it/EmbeddingMojoIT/embed_dependency_simple_app/.m2/repository"); + try (JarFile m2RepositoryJar = new JarFile(m2Repository.resolve("org/apache/commons/commons-lang3/3.17.0/commons-lang3-3.17.0.jar").toFile())) { + HashMap m2RepositorySbom = CorrectnessAnalyser.getSBOM(m2RepositoryJar); + assertEquals(0, m2RepositorySbom.size()); + } catch (IOException e) { + throw new RuntimeException("Failed to open m2 repository jar", e); + } } - }