Skip to content

Commit 6448c48

Browse files
committed
feat: use artifact.setFile to point to embedded dependency
1 parent ef33f0c commit 6448c48

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

maven-plugin/src/main/java/io/github/project/classport/plugin/EmbeddingMojo.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import io.github.project.classport.commons.ClassportInfo;
3535
import 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)
3838
public 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(),

maven-plugin/src/test/java/io/github/project/classport/plugin/it/EmbeddingMojoIT.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.stream.Collectors;
1010

1111
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import org.junit.jupiter.api.Disabled;
1312

1413
import com.soebes.itf.jupiter.extension.MavenGoal;
1514
import com.soebes.itf.jupiter.extension.MavenJupiterExtension;
@@ -20,7 +19,6 @@
2019
import io.github.project.classport.analyser.CorrectnessAnalyser;
2120
import io.github.project.classport.commons.ClassportInfo;
2221

23-
@Disabled("Skipping this test until we find a way to embed using a single command")
2422
@MavenJupiterExtension
2523
public class EmbeddingMojoIT {
2624
@MavenGoal("package")
@@ -44,6 +42,14 @@ void embed_dependency_simple_app(MavenExecutionResult result) {
4442
Set<String> expectedIds = Set.of("org.apache.commons:commons-lang3:jar:3.17.0", "org.example:hello:jar:0.1.0");
4543
Set<String> actualIds = sbom.values().stream().map(ClassportInfo::id).collect(Collectors.toSet());
4644
assertEquals(expectedIds, actualIds);
45+
// Make sure that dependency in m2 repository is not embedded
46+
Path homeDirectory = Paths.get(System.getProperty("user.home"));
47+
Path m2Repository = homeDirectory.resolve(".m2/repository");
48+
try (JarFile m2RepositoryJar = new JarFile(m2Repository.resolve("org/apache/commons/commons-lang3/3.17.0/commons-lang3-3.17.0.jar").toFile())) {
49+
HashMap<String, ClassportInfo> m2RepositorySbom = CorrectnessAnalyser.getSBOM(m2RepositoryJar);
50+
assertEquals(0, m2RepositorySbom.size());
51+
} catch (IOException e) {
52+
throw new RuntimeException("Failed to open m2 repository jar", e);
53+
}
4754
}
48-
4955
}

0 commit comments

Comments
 (0)