Skip to content

Commit

Permalink
Skip the transformer if log4j-api isn't present
Browse files Browse the repository at this point in the history
Signed-off-by: Ralph Goers <[email protected]>
  • Loading branch information
rgoers committed Oct 15, 2024
1 parent ff0fd35 commit fbf2ff7
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.logging.log4j.transform.maven.scan.ClassFileInclusionScanner;
Expand Down Expand Up @@ -106,7 +107,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Skipping project with packaging \"pom\".");
return;
}
validateLog4jVersion();
if (!validateLog4jVersion()) {
return;
}

final Path sourceDirectory = this.sourceDirectory.toPath();
final Path outputDirectory = this.outputDirectory.toPath();
Expand Down Expand Up @@ -189,11 +192,17 @@ private WrappedIOException(IOException cause) {
}
}

private void validateLog4jVersion() throws MojoExecutionException {
Artifact log4jApi = project.getArtifacts().stream()
private boolean validateLog4jVersion() throws MojoExecutionException {
Optional<Artifact> artifact = project.getArtifacts().stream()
.filter(a -> LOG4J_GROUP_ID.equals(a.getGroupId()) && LOG4J_API_ARTIFACT_ID.equals(a.getArtifactId()))
.findAny()
.orElseThrow(() -> new MojoExecutionException("Missing `log4j-api` dependency."));
.findAny();
Artifact log4jApi;
if (artifact.isPresent()) {
log4jApi = artifact.get();
} else {
getLog().info("Skipping project. Log4j is not being used.");
return false;
}
try {
if (MIN_SUPPORTED_VERSION.compareTo(log4jApi.getSelectedVersion()) > 0) {
throw new MojoExecutionException("Log4j2 API version " + MIN_SUPPORTED_VERSION
Expand All @@ -206,6 +215,7 @@ private void validateLog4jVersion() throws MojoExecutionException {
} catch (OverConstrainedVersionException e) {
throw new MojoExecutionException("Can not determine `log4j-api` version.", e);
}
return true;
}

private ClassLoader getProjectDependencies() throws MojoExecutionException {
Expand Down

0 comments on commit fbf2ff7

Please sign in to comment.