use of org.apache.maven.model.building.DefaultModelBuilder in project meghanada-server by mopemope.
the class POMParser method parsePom.
POMInfo parsePom(File pom) throws ProjectParseException {
try {
if (!pom.isAbsolute()) {
pom = pom.getCanonicalFile();
}
ModelBuildingRequest req = new DefaultModelBuildingRequest();
req.setPomFile(pom);
req.setSystemProperties(System.getProperties());
req.setModelResolver(new LocalModelResolver(this.projectRoot));
req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
req.setProcessPlugins(true);
DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
DefaultModelBuilder builder = factory.newInstance();
ModelBuildingResult result = builder.build(req);
Model mavenModel = result.getEffectiveModel();
POMInfo pomInfo = new POMInfo(pom.getParent());
String groupId = mavenModel.getGroupId();
if (nonNull(groupId)) {
pomInfo.groupId = groupId;
}
String artifactId = mavenModel.getArtifactId();
if (nonNull(artifactId)) {
pomInfo.artifactId = artifactId;
}
String version = mavenModel.getVersion();
if (nonNull(version)) {
pomInfo.version = version;
}
Properties modelProperties = mavenModel.getProperties();
if (nonNull(modelProperties)) {
if (nonNull(groupId)) {
modelProperties.put("project.groupId", groupId);
}
if (nonNull(artifactId)) {
modelProperties.put("project.artifactId", artifactId);
}
if (nonNull(version)) {
modelProperties.put("project.version", version);
}
for (String key : modelProperties.stringPropertyNames()) {
String value = modelProperties.getProperty(key);
if (nonNull(value)) {
pomInfo.properties.setProperty(key, value);
}
}
POMParser.replacePOMProperties(pomInfo);
}
Build build = mavenModel.getBuild();
this.parseBuild(pomInfo, build);
return pomInfo;
} catch (IOException | ModelBuildingException e) {
throw new ProjectParseException(e);
}
}
use of org.apache.maven.model.building.DefaultModelBuilder in project spring-boot by spring-projects.
the class DependencyManagementBomTransformation method updateDependencyResolutionContext.
private void updateDependencyResolutionContext(List<Map<String, String>> bomDependencies) {
URI[] uris = Grape.getInstance().resolve(null, bomDependencies.toArray(new Map<?, ?>[0]));
DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
for (URI uri : uris) {
try {
DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
request.setModelResolver(new GrapeModelResolver());
request.setModelSource(new org.apache.maven.model.building.UrlModelSource(uri.toURL()));
request.setSystemProperties(System.getProperties());
Model model = modelBuilder.build(request).getEffectiveModel();
this.resolutionContext.addDependencyManagement(new MavenModelDependencyManagement(model));
} catch (Exception ex) {
throw new IllegalStateException("Failed to build model for '" + uri + "'. Is it a valid Maven bom?", ex);
}
}
}
Aggregations