Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support annotation processor dependencies #7679

Merged
merged 3 commits into from
Aug 26, 2024
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 @@ -397,4 +397,16 @@ public void testInsertMultipleDependencies() throws Exception {
// dependency is NOT listed for compile
assertContainsDependency(false, Scopes.COMPILE, art);
}

public void testAddAnnotationProcessor() throws Exception {
makeProject("projects/micronaut", "build2.gradle");
ArtifactSpec art = ArtifactSpec.make("io.micronaut.data", "micronaut-data-processor");
Dependency toAdd = Dependency.make(art, Scopes.PROCESS);
DependencyChange change = DependencyChange.builder(DependencyChange.Kind.ADD).
dependency(toAdd).
create();
executeChangeAndWait(change);
// dependency is listed for runtime
assertContainsDependency(true, Scopes.PROCESS, art);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.xml.namespace.QName;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.netbeans.api.project.Project;
import org.netbeans.modules.maven.api.Constants;
import org.netbeans.modules.maven.api.NbMavenProject;
import org.netbeans.modules.maven.api.PluginPropertyUtils;
import org.netbeans.modules.maven.model.pom.Build;
import org.netbeans.modules.maven.model.pom.Configuration;
import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
import org.netbeans.modules.maven.model.pom.POMModel;
import org.netbeans.modules.maven.model.pom.Plugin;
import org.netbeans.modules.maven.options.MavenVersionSettings;
import org.netbeans.modules.project.dependency.ArtifactSpec;
import org.netbeans.modules.project.dependency.Dependency;
import org.netbeans.modules.project.dependency.DependencyChange;
Expand All @@ -35,16 +46,26 @@
import org.netbeans.modules.project.dependency.ProjectDependencies;
import org.netbeans.modules.project.dependency.ProjectOperationException;
import org.netbeans.modules.project.dependency.Scopes;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

/**
*
* @author sdedic
*/
public class DependencyAdder {
private static final String ELEMENT_VERSION = "version"; // NOI18N
private static final String ELEMENT_ARTIFACT_ID = "artifactId"; // NOI18N
private static final String ELEMENT_GROUP_ID = "groupId"; // NOI18N
private static final String ELEMENT_PATH = "path"; // NOI18N
private static final String ELEMENT_PROCESSOR_PATHS = "annotationProcessorPaths"; // NOI18N

private final Project project;
private final DependencyChange request;
private final RewriteContext rewrite;
// will be initialized during execute()
private POMModel mutableModel;

public DependencyAdder(Project project, DependencyChange request, RewriteContext rewrite) {
this.project = project;
Expand All @@ -71,21 +92,36 @@ protected void recordConflict(Dependency requested, Dependency existing) {
}
}

protected boolean checkDependencyConflicts(Dependency existing, Dependency d) throws DependencyChangeException {
ArtifactSpec existingA = existing.getArtifact();
ArtifactSpec toAdd = d.getArtifact();

private ArtifactSpec mavenToArtifactSpec(Artifact a) {
FileObject f = a.getFile() == null ? null : FileUtil.toFileObject(a.getFile());
if (a.isSnapshot()) {
return ArtifactSpec.createSnapshotSpec(a.getGroupId(), a.getArtifactId(),
a.getType(), a.getClassifier(), a.getVersion(), a.isOptional(), f, a);
} else {
return ArtifactSpec.createVersionSpec(a.getGroupId(), a.getArtifactId(),
a.getType(), a.getClassifier(), a.getVersion(), a.isOptional(), f, a);
}
}

protected boolean checkDependencyConflicts(ArtifactSpec existingA, ArtifactSpec toAdd) throws DependencyChangeException {
if (!(existingA.getGroupId().equals(toAdd.getGroupId()) &&
existingA.getArtifactId().equals(toAdd.getArtifactId()))) {
// different artifacts -> no conflicts
return true;
}
String existingC = existingA.getClassifier();
if (existingA != null) {
if (existingC != null) {
if (!Objects.equals(existingC, toAdd.getClassifier())) {
return true;
}
}
return false;
}

protected boolean checkDependencyConflicts(Dependency existing, Dependency d) throws DependencyChangeException {
if (checkDependencyConflicts(existing.getArtifact(), d.getArtifact())) {
return true;
}
String mavenScope = rewrite.mavenScope(d);
String existingScope = rewrite.mavenScope(existing);
if (!mavenScope.equals(existingScope)) {
Expand All @@ -98,15 +134,73 @@ protected boolean checkDependencyConflicts(Dependency existing, Dependency d) th

DependencyResult current;

private void addProcessorDependency(Dependency d) {
ArtifactSpec a = d.getArtifact();
Build pBuild = mutableModel.getProject().getBuild();
if (pBuild == null) {
pBuild = mutableModel.getFactory().createBuild();
mutableModel.getProject().setBuild(pBuild);
}
Plugin plg = pBuild.findPluginById(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER);
if (plg == null) {
plg = mutableModel.getFactory().createPlugin();
plg.setGroupId(Constants.GROUP_APACHE_PLUGINS);
plg.setArtifactId(Constants.PLUGIN_COMPILER);
plg.setVersion(MavenVersionSettings.getDefault().getVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER));
pBuild.addPlugin(plg);
}
Configuration config = plg.getConfiguration();
if (config == null) {
config = mutableModel.getFactory().createConfiguration();
plg.setConfiguration(config);
}
Configuration fConfig = config;
POMExtensibilityElement annoPathList =
config.getConfigurationElements().stream().filter(e -> e.getQName().getLocalPart().equals(ELEMENT_PROCESSOR_PATHS)).
findAny().orElseGet(() -> {
POMExtensibilityElement e = mutableModel.getFactory().createPOMExtensibilityElement(new QName(ELEMENT_PROCESSOR_PATHS));
fConfig.addExtensibilityElement(e);
return e;
});
POMExtensibilityElement path = mutableModel.getFactory().createPOMExtensibilityElement(new QName(ELEMENT_PATH));
path.setChildElementText(null, a.getGroupId(), new QName(ELEMENT_GROUP_ID));
path.setChildElementText(null, a.getArtifactId(), new QName(ELEMENT_ARTIFACT_ID));
if (a.getVersionSpec() != null) {
path.setChildElementText(null, a.getVersionSpec(), new QName(ELEMENT_VERSION));
}
annoPathList.addExtensibilityElement(path);
}

private void addCodeDependency(Dependency d) {
ArtifactSpec a = d.getArtifact();
org.netbeans.modules.maven.model.pom.Dependency mavenDep = mutableModel.getFactory().createDependency();
mavenDep.setGroupId(a.getGroupId());
mavenDep.setArtifactId(a.getArtifactId());
if (a.getVersionSpec() != null && !a.getVersionSpec().isEmpty()) {
mavenDep.setVersion(a.getVersionSpec());
}
if (a.getClassifier() != null) {
mavenDep.setClassifier(a.getClassifier());
}
String scope = rewrite.mavenScope(d);
if (!"compile".equals(scope)) {
mavenDep.setScope(scope);
}
mutableModel.getProject().addDependency(mavenDep);
}

@NbBundle.Messages({
"ERR_AddingDependency=Error adding dependency"
})
public void execute() throws DependencyChangeException {
current = ProjectDependencies.findDependencies(project, ProjectDependencies.newQuery(Scopes.DECLARED));

NbMavenProject nbmp = project.getLookup().lookup(NbMavenProject.class);
MavenProject mp = nbmp.getMavenProject();
Map<String, org.apache.maven.model.Plugin> plugins = mp.getBuild().getPluginsAsMap();
// lazy initialized if PROCESS dependency is found
List<ArtifactSpec> annotationPath = null;
try {

POMModel mutableModel = rewrite.getWriteModel();
mutableModel = rewrite.getWriteModel();
for (Dependency d : request.getDependencies()) {
boolean toAccept = true;
for (Dependency c : current.getRoot().getChildren()) {
Expand All @@ -115,6 +209,31 @@ public void execute() throws DependencyChangeException {
break;
}
}
// check annotation processors, they are elsewhere in the POM
if (d.getScope().equals(Scopes.PROCESS)) {
if (annotationPath == null) {
// lazy init
PluginPropertyUtils.PluginConfigPathParams params = new PluginPropertyUtils.PluginConfigPathParams(
Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER, ELEMENT_PROCESSOR_PATHS, ELEMENT_PATH);
List<Artifact> arts = PluginPropertyUtils.getPluginPathProperty(project, params, true, null);
if (arts != null) {
annotationPath = PluginPropertyUtils.getPluginPathProperty(project, params, true, null).stream().map(this::mavenToArtifactSpec).toList();
}
if (annotationPath == null) {
annotationPath = Collections.emptyList();
}
}
for (ArtifactSpec a : annotationPath) {
if (d.getArtifact() == null) {
// TODO: handle projects
continue;
}
if (!checkDependencyConflicts(a, d.getArtifact())) {
toAccept = false;
break;
}
}
}
if (toAccept) {
accepted.add(d);
}
Expand All @@ -127,21 +246,11 @@ public void execute() throws DependencyChangeException {
boolean ok = false;
try {
for (Dependency d : accepted) {
ArtifactSpec a = d.getArtifact();
org.netbeans.modules.maven.model.pom.Dependency mavenDep = mutableModel.getFactory().createDependency();
mavenDep.setGroupId(a.getGroupId());
mavenDep.setArtifactId(a.getArtifactId());
if (a.getVersionSpec() != null && !a.getVersionSpec().isEmpty()) {
mavenDep.setVersion(a.getVersionSpec());
}
if (a.getClassifier() != null) {
mavenDep.setClassifier(a.getClassifier());
}
String scope = rewrite.mavenScope(d);
if (!"compile".equals(scope)) {
mavenDep.setScope(scope);
if (d.getScope().equals(Scopes.PROCESS)) {
addProcessorDependency(d);
} else {
addCodeDependency(d);
}
mutableModel.getProject().addDependency(mavenDep);
}
mutableModel.endTransaction();
ok = true;
Expand Down

This file was deleted.

Loading
Loading