Java Code Examples for org.apache.maven.model.Parent#getArtifactId()

The following examples show how to use org.apache.maven.model.Parent#getArtifactId() . 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: LocationAwareMavenXpp3Writer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void writeParent(Parent parent, String tagName, XmlSerializer serializer)
        throws java.io.IOException {
    serializer.startTag(NAMESPACE, tagName);
    flush(serializer);
    StringBuffer b = b(serializer);
    int start = b.length();
    if (parent.getGroupId() != null) {
        writeValue(serializer, "groupId", parent.getGroupId(), parent);
    }
    if (parent.getArtifactId() != null) {
        writeValue(serializer, "artifactId", parent.getArtifactId(), parent);
    }
    if (parent.getVersion() != null) {
        writeValue(serializer, "version", parent.getVersion(), parent);
    }
    if ((parent.getRelativePath() != null) && !parent.getRelativePath().equals("../pom.xml")) {
        writeValue(serializer, "relativePath", parent.getRelativePath(), parent);
    }
    serializer.endTag(NAMESPACE, tagName).flush();
    logLocation(parent, "", start, b.length());
}
 
Example 3
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 4
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the POM for the specified parent.
 *
 * @param parent the parent coordinates to resolve, must not be {@code null}
 * @return The source of the requested POM, never {@code null}
 * @since Apache-Maven-3.2.2 (MNG-5639)
 */
public ModelSource resolveModel( Parent parent )
    throws UnresolvableModelException
{

    String groupId = parent.getGroupId();
    String artifactId = parent.getArtifactId();
    String version = parent.getVersion();

    // resolve version range (if present)
    if ( isRestrictedVersionRange( version, groupId, artifactId ) )
    {
        version = resolveParentVersionRange( groupId, artifactId, version );
    }

    return resolveModel( groupId, artifactId, version );
}
 
Example 5
Source File: ParentPOMPropagatingArtifactDescriptorReaderDelegate.java    From mvn2nix-maven-plugin with MIT License 6 votes vote down vote up
@Override
public void populateResult(RepositorySystemSession session,
	ArtifactDescriptorResult result,
	Model model) {
	super.populateResult(session, result, model);
	Parent parent = model.getParent();
	if (parent != null) {
		DefaultArtifact art =
			new DefaultArtifact(parent.getGroupId(),
				parent.getArtifactId(),
				"pom",
				parent.getVersion());
		Dependency dep = new Dependency(art, "compile");
		result.addDependency(dep);
	}
}
 
Example 6
Source File: ArtifactServerUtil.java    From LicenseScout with Apache License 2.0 5 votes vote down vote up
private URL getParentUrl(final Parent parent) throws MalformedURLException {
    final String groupId = parent.getGroupId();
    final String artifactId = parent.getArtifactId();
    final String version = parent.getVersion();
    final String urlString = getArtifactUrlString(groupId, artifactId, version);
    return new URL(urlString);
}
 
Example 7
Source File: LicenseFinder.java    From sonarqube-licensecheck with Apache License 2.0 5 votes vote down vote up
public static List<License> getLicenses(File filePath, String userSettings, String globalSettings)
{
    try
    {
        Model model = new MavenXpp3Reader().read(new FileInputStream(filePath));

        if (!model.getLicenses().isEmpty())
        {
            return model.getLicenses();
        }
        else
        {
            if (model.getParent() != null)
            {
                Parent parent = model.getParent();
                Dependency dependency =
                    new Dependency(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion(), null);
                return getLicenses(DirectoryFinder.getPomPath(dependency,
                    DirectoryFinder.getMavenRepsitoryDir(userSettings, globalSettings)),
                    userSettings, globalSettings);
            }
            else
            {
                return Collections.emptyList();
            }
        }

    }
    catch (Exception e)
    {
        LOGGER.warn("Could not parse Maven POM " + filePath, e);
        return Collections.emptyList();
    }
}
 
Example 8
Source File: GAV.java    From maven-git-versioning-extension with MIT License 5 votes vote down vote up
public static GAV of(Parent parent) {

        String groupId = parent.getGroupId();
        String artifactId = parent.getArtifactId();
        String version = parent.getVersion();

        return new GAV(groupId, artifactId, version);
    }