Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand All @@ -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<Artifact> 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);
Expand All @@ -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);
}

}
}

Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -44,6 +42,13 @@ void embed_dependency_simple_app(MavenExecutionResult result) {
Set<String> expectedIds = Set.of("org.apache.commons:commons-lang3:jar:3.17.0", "org.example:hello:jar:0.1.0");
Set<String> 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<String, ClassportInfo> m2RepositorySbom = CorrectnessAnalyser.getSBOM(m2RepositoryJar);
assertEquals(0, m2RepositorySbom.size());
} catch (IOException e) {
throw new RuntimeException("Failed to open m2 repository jar", e);
}
}

}