Skip to content

Commit

Permalink
Adds support to -processorpath
Browse files Browse the repository at this point in the history
Ported from maven-compiler-plugin
  • Loading branch information
ulyssesrr committed Jun 23, 2021
1 parent 8de3204 commit 7caf6b7
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResult;

import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticListener;
Expand Down Expand Up @@ -123,6 +127,35 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
*/
@Parameter
private File outputDirectory;

/**
* <p>
* Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation
* processors only in those classpath elements. If omitted, the default classpath is used to detect annotation
* processors. The detection itself depends on the configuration of {@code processors}.
* </p>
* <p>
* Each classpath element is specified using their Maven coordinates (groupId, artifactId, version, classifier,
* type). Transitive dependencies are added automatically. Example:
* </p>
*
* <pre>
* &lt;configuration&gt;
* &lt;annotationProcessorPaths&gt;
* &lt;path&gt;
* &lt;groupId&gt;org.sample&lt;/groupId&gt;
* &lt;artifactId&gt;sample-annotation-processor&lt;/artifactId&gt;
* &lt;version&gt;1.2.3&lt;/version&gt;
* &lt;/path&gt;
* &lt;!-- ... more ... --&gt;
* &lt;/annotationProcessorPaths&gt;
* &lt;/configuration&gt;
* </pre>
*
* @since 5.0
*/
@Parameter
private List<DependencyCoordinate> annotationProcessorPaths;

/**
* Annotation Processor FQN (Full Qualified Name) - when processors are not specified, the default discovery mechanism will be used
Expand Down Expand Up @@ -449,7 +482,8 @@ private String buildModulePath()
/**
*
*/
public void execute() throws MojoExecutionException
@Override
public void execute() throws MojoExecutionException
{
if (skip)
{
Expand Down Expand Up @@ -530,7 +564,7 @@ private Toolchain getToolchain(final Map<String, String> jdkToolchain)
return tc;
}

private List<String> prepareOptions( JavaCompiler compiler ) {
private List<String> prepareOptions( JavaCompiler compiler ) throws MojoExecutionException {

final List<String> options = new ArrayList<>(10);

Expand All @@ -552,6 +586,12 @@ private List<String> prepareOptions( JavaCompiler compiler ) {
});

options.add("-proc:only");

Optional<String> processorPath = this.buildProcessorPath();
processorPath.ifPresent(value -> {
options.add("-processorpath");
options.add(value);
});

addCompilerArguments(options);

Expand Down Expand Up @@ -1000,4 +1040,47 @@ private void processSourceArtifacts( Consumer<Artifact> closure ) {
}
}

private Optional<List<String>> resolveProcessorPathEntries() throws MojoExecutionException {
if (this.annotationProcessorPaths == null || this.annotationProcessorPaths.isEmpty()) {
return Optional.empty();
}

try {
Set<Dependency> requiredDependencies = new LinkedHashSet<>();

for (DependencyCoordinate coord : this.annotationProcessorPaths) {
//@formatter:off
DefaultArtifact artifact = new DefaultArtifact(
coord.getGroupId(),
coord.getArtifactId(),
coord.getClassifier(),
coord.getType(),
coord.getVersion());
//@formatter:on

requiredDependencies.add(new Dependency(artifact, Artifact.SCOPE_RUNTIME));
}
CollectRequest collectRequest = new CollectRequest(requiredDependencies.iterator().next(),
new ArrayList<>(requiredDependencies), this.remoteRepos);
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);

DependencyResult resolutionResult = this.repoSystem.resolveDependencies(this.repoSession,
dependencyRequest);

List<String> artifactPaths = new ArrayList<>(resolutionResult.getArtifactResults().size());
for (ArtifactResult artifactResult : resolutionResult.getArtifactResults()) {
artifactPaths.add(artifactResult.getArtifact().getFile().getAbsolutePath());
}

return Optional.of(artifactPaths);
} catch (Exception e) {
throw new MojoExecutionException(
"Resolution of annotationProcessorPath dependencies failed: " + e.getLocalizedMessage(), e);
}
}

private Optional<String> buildProcessorPath() throws MojoExecutionException {
Optional<List<String>> processorPathEntries = this.resolveProcessorPathEntries();
return processorPathEntries.map(value -> StringUtils.join(value.iterator(), File.pathSeparator));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.bsc.maven.plugin.processor;

import java.util.Objects;

/**
* Maven-coordinates of a dependency.
*
* @author Ulysses R. Ribeiro
*
*/
public class DependencyCoordinate {

private String groupId;

private String artifactId;

private String version;

private String classifier;

private String type = "jar";

public String getGroupId() {
return this.groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}

public String getArtifactId() {
return this.artifactId;
}

public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}

public String getVersion() {
return this.version;
}

public void setVersion(String version) {
this.version = version;
}

public String getClassifier() {
return this.classifier;
}

public void setClassifier(String classifier) {
this.classifier = classifier;
}

public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}

@Override
public int hashCode() {
return Objects.hash(artifactId, classifier, groupId, type, version);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
DependencyCoordinate other = (DependencyCoordinate) obj;
return Objects.equals(artifactId, other.artifactId) && Objects.equals(classifier, other.classifier)
&& Objects.equals(groupId, other.groupId) && Objects.equals(type, other.type)
&& Objects.equals(version, other.version);
}

@Override
public String toString() {
return "DependencyCoordinate [groupId=" + groupId + ", artifactId=" + artifactId + ", version=" + version
+ ", classifier=" + classifier + ", type=" + type + "]";
}

}

0 comments on commit 7caf6b7

Please sign in to comment.