Java Code Examples for org.apache.maven.project.MavenProject#getModel()

The following examples show how to use org.apache.maven.project.MavenProject#getModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenHelper.java    From cyclonedx-gradle-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the parent pom for an artifact (if any). The parent pom may contain license,
 * description, and other metadata whereas the artifact itself may not.
 * @param artifact the artifact to retrieve the parent pom for
 * @param project the maven project the artifact is part of
 */
private MavenProject retrieveParentProject(ResolvedArtifact artifact, MavenProject project) {
    if (artifact.getFile() == null || artifact.getFile().getParentFile() == null || !isDescribedArtifact(artifact)) {
        return null;
    }
    final Model model = project.getModel();
    if (model.getParent() != null) {
        final Parent parent = model.getParent();
        // Navigate out of version, artifactId, and first (possibly only) level of groupId
        final StringBuilder getout = new StringBuilder("../../../");
        final ModuleVersionIdentifier mid = artifact.getModuleVersion().getId();
        final int periods = mid.getGroup().length() - mid.getGroup().replace(".", "").length();
        for (int i= 0; i< periods; i++) {
            getout.append("../");
        }
        final File parentFile = new File(artifact.getFile().getParentFile(), getout + parent.getGroupId().replace(".", "/") + "/" + parent.getArtifactId() + "/" + parent.getVersion() + "/" + parent.getArtifactId() + "-" + parent.getVersion() + ".pom");
        if (parentFile.exists() && parentFile.isFile()) {
            try {
                return readPom(parentFile.getCanonicalFile());
            } catch (Exception e) {
                logger.error("An error occurred retrieving an artifacts parent pom", e);
            }
        }
    }
    return null;
}
 
Example 2
Source File: BaseCycloneDxMojo.java    From cyclonedx-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the parent pom for an artifact (if any). The parent pom may contain license,
 * description, and other metadata whereas the artifact itself may not.
 * @param artifact the artifact to retrieve the parent pom for
 * @param project the maven project the artifact is part of
 */
private MavenProject retrieveParentProject(final Artifact artifact, final MavenProject project) {
    if (artifact.getFile() == null || artifact.getFile().getParentFile() == null || !isDescribedArtifact(artifact)) {
        return null;
    }
    final Model model = project.getModel();
    if (model.getParent() != null) {
        final Parent parent = model.getParent();
        // Navigate out of version, artifactId, and first (possibly only) level of groupId
        final StringBuilder getout = new StringBuilder("../../../");
        final int periods = artifact.getGroupId().length() - artifact.getGroupId().replace(".", "").length();
        for (int i= 0; i< periods; i++) {
            getout.append("../");
        }
        final File parentFile = new File(artifact.getFile().getParentFile(), getout + parent.getGroupId().replace(".", "/") + "/" + parent.getArtifactId() + "/" + parent.getVersion() + "/" + parent.getArtifactId() + "-" + parent.getVersion() + ".pom");
        if (parentFile.exists() && parentFile.isFile()) {
            try {
                return readPom(parentFile.getCanonicalFile());
            } catch (Exception e) {
                getLog().error("An error occurred retrieving an artifacts parent pom", e);
            }
        }
    }
    return null;
}
 
Example 3
Source File: ProjectManifestCustomizer.java    From vertx-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> getEntries(AbstractVertxMojo mojo, MavenProject project) {
    Map<String, String> attributes = new HashMap<>();
    Model model = project.getModel();

    attributes.put(ExtraManifestKeys.PROJECT_ARTIFACT_ID.header(), model.getArtifactId());
    attributes.put(ExtraManifestKeys.PROJECT_GROUP_ID.header(), model.getGroupId());
    attributes.put(ExtraManifestKeys.PROJECT_VERSION.header(), model.getVersion());
    attributes.put(ExtraManifestKeys.PROJECT_NAME.header(),
        model.getName() == null ? model.getArtifactId() : model.getName());

    attributes.put(ExtraManifestKeys.BUILD_TIMESTAMP.header(), manifestTimestampFormat(new Date()));

    if (project.getUrl() != null) {
        attributes.put(ExtraManifestKeys.PROJECT_URL.header(), project.getUrl());
    }

    // TODO get the filtered lists.
    List<Dependency> dependencies = model.getDependencies();
    if (dependencies != null && !dependencies.isEmpty()) {
        String deps = dependencies.stream()
            .filter(d -> "compile".equals(d.getScope()) || null == d.getScope())
            .map(ProjectManifestCustomizer::asCoordinates)
            .collect(Collectors.joining(" "));
        attributes.put(ExtraManifestKeys.PROJECT_DEPS.header(), deps);
    }

    return attributes;
}
 
Example 4
Source File: ProjectManifestCustomizer.java    From vertx-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> getEntries(PackageMojo mojo, MavenProject project) {
    Map<String, String> attributes = new HashMap<>();
    Model model = project.getModel();

    attributes.put("Project-Name",
        model.getName() == null ? model.getArtifactId() : model.getName());
    attributes.put("Project-Group", model.getGroupId());
    attributes.put("Project-Version", model.getVersion());
    attributes.put("Build-Timestamp", manifestTimestampFormat(new Date()));

    if (project.getUrl() != null) {
        attributes.put("Project-Url", model.getUrl());
    }

    // TODO get the filtered lists.
    List<Dependency> dependencies = model.getDependencies();
    if (dependencies != null && !dependencies.isEmpty()) {
        String deps = dependencies.stream()
            .filter(d -> "compile".equals(d.getScope()) || null == d.getScope())
            .map(ProjectManifestCustomizer::asCoordinates)
            .collect(Collectors.joining(" "));
        attributes.put("Project-Dependencies", deps);
    }

    return attributes;
}
 
Example 5
Source File: SimpleReactorReader.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Model findModel(Artifact artifact) {
  MavenProject project = getProject(artifact);
  if (project != null) {
    return project.getModel();
  }
  return null;
}
 
Example 6
Source File: MavenUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
private static void initializeBuildModel(MavenProject mavenProject) {
	Model model = mavenProject.getModel();
	if (model.getBuild()!=null) {
		model.setBuild(new Build());
	}
}