in buildutils/buildutils-maven-plugin/src/main/java/org/apache/axiom/buildutils/sources/PostProcessMojo.java [64:129]
public void execute() throws MojoExecutionException, MojoFailureException {
if (project.getPackaging().equals("pom")) {
return;
}
Set<String> sources = new HashSet<>();
try (JarInputStream in =
new JarInputStream(new FileInputStream(project.getArtifact().getFile()))) {
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
String name = entry.getName();
if (name.endsWith(".class")) {
new ClassReader(in)
.accept(
new SourceExtractor(
sources, name.substring(0, name.lastIndexOf('/') + 1)),
ClassReader.SKIP_CODE);
}
}
} catch (IOException ex) {
throw new MojoExecutionException("Error reading jar: " + ex.getMessage(), ex);
}
File sourcesJar =
new File(
project.getBuild().getDirectory(),
project.getBuild().getFinalName() + "-sources.jar");
File postProcessedSourcesJar =
new File(
project.getBuild().getDirectory(),
project.getBuild().getFinalName() + "-post-processed-sources.jar");
try (JarOutputStream out =
new JarOutputStream(new FileOutputStream(postProcessedSourcesJar))) {
processSourceJar(sourcesJar, sources, true, out);
ArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
for (Artifact artifact : project.getArtifacts()) {
if (sources.isEmpty()) {
break;
}
if (filter.include(artifact)) {
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(artifact.getGroupId());
coordinate.setArtifactId(artifact.getArtifactId());
coordinate.setVersion(artifact.getVersion());
coordinate.setExtension("jar");
coordinate.setClassifier("sources");
Artifact resolvedArtifact;
try {
resolvedArtifact =
artifactResolver
.resolveArtifact(
session.getProjectBuildingRequest(), coordinate)
.getArtifact();
} catch (ArtifactResolverException ex) {
getLog().warn("Could not get sources for " + artifact);
continue;
}
if (resolvedArtifact.isResolved()) {
processSourceJar(resolvedArtifact.getFile(), sources, false, out);
}
}
}
} catch (IOException ex) {
throw new MojoExecutionException("Error writing jar: " + ex.getMessage(), ex);
}
sourcesJar.delete();
postProcessedSourcesJar.renameTo(sourcesJar);
}