简体   繁体   中英

DefaultProjectBuildingRequest org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs

I'm trying to programmatically resolve a pom , but I'm encountering several errors.

Here's a snippet of the code:


ContainerConfiguration config = new DefaultContainerConfiguration();
config.setAutoWiring(true);
config.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
PlexusContainer plexusContainer = new DefaultPlexusContainer(config);
ProjectBuilder projectBuilder = plexusContainer.lookup(ProjectBuilder.class);
RepositorySystem repositorySystem = plexusContainer.lookup(RepositorySystem.class);
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepository = new LocalRepository("target/.m2");
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));

DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
request.setRepositorySession(session);
request.setResolveDependencies(true);
ArtifactRepository centralRepository = new MavenArtifactRepository();
centralRepository.setUrl("https://repo.maven.apache.org/maven2/");
centralRepository.setLayout(new DefaultRepositoryLayout());
request.setRemoteRepositories(Collections.singletonList(centralRepository));

ProjectBuildingResult result = projectBuilder.build(new File("pom.xml"), request);

result.getDependencyResolutionResult().getDependencies().forEach(dependency ->
    System.out.println(
        "Provided pom depends on: " +
            dependency.getArtifact().getGroupId() +
            ":" + dependency.getArtifact().getGroupId() +
            ":" + dependency.getArtifact().getVersion()
        )
);

but unfortunately I end up with such errors while executing projectBuilder.build(new File("pom.xml"), request);

Caused by: org.apache.maven.model.building.ModelBuildingException: 
15 problems were encountered while building the effective model for net.dahanne:maven-resolver:0.0.1-SNAPSHOT
[ERROR] Invalid artifact repository: null @ 
[ERROR] Failed to determine Java version for profile jdk8 @ io.dropwizard.metrics:metrics-parent:4.1.17, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/io/dropwizard/metrics/metrics-parent/4.1.17/metrics-parent-4.1.17.pom, line 160, column 22
[ERROR] Failed to determine Java version for profile doclint-java8-disable @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 839, column 17
[ERROR] Failed to determine Java version for profile compile-java8-release-flag @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 879, column 18
[ERROR] Failed to determine Java version for profile include-jdk-misc @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 910, column 22
[ERROR] Failed to determine Java version for profile java8-test @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 961, column 22
[ERROR] Failed to determine Java version for profile java9-mr-build @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 998, column 22
[ERROR] Failed to determine Java version for profile java9-test @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1047, column 22
[ERROR] Failed to determine Java version for profile java10-mr-build @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1087, column 22
[ERROR] Failed to determine Java version for profile java10-test @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1139, column 22
[ERROR] Failed to determine Java version for profile java11-mr-build @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1182, column 22
[ERROR] Failed to determine Java version for profile java11-test @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1237, column 22
[ERROR] Failed to determine Java version for profile java12-mr-build @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1283, column 22
[ERROR] Failed to determine Java version for profile java12-test @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1341, column 22
[ERROR] Failed to determine Java version for profile java13-mr-build @ org.jboss:jboss-parent:36, /Users/anthony.dahanne/workspaces/maven-resolver/target/.m2/org/jboss/jboss-parent/36/jboss-parent-36.pom, line 1390, column 22

It looks as if the maven resolver could not set the properties of the poms that are being traversed...

I have a full github project to demo the issue , including CI .

I also have crossposted on Maven dev mailing list .

Thanks!

When you assembling your request, you need to pass the Java version as a system property. What worked for me is something similar:

final Properties systemProperties = new Properties();
systemProperties.setProperty("java.version", "1.8");
final DefaultProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest();
projectBuildingRequest.setSystemProperties(systemProperties);

The problem comes from the JdkVersionProfileActivator class which explicitly checks the value for the java.version property from the system properties.

Edit: as I just checked the linked Github issue, someone else already sent a PR with a similar solution with some additional improvement like using System.getProperties() to get the properties instead of manually creating the object for it.

Hervé Boutemy sent the fix via a PR on the github repo where I demo'ed the issue - I missed some configuration indeed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM