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

The following examples show how to use org.apache.maven.model.Parent#getVersion() . 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: CommandRequirements.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
/**
 * Require that a valid Maven project configuration exists.
 *
 * @param commonOptions The options.
 */
static void requireValidMavenProjectConfig(CommonOptions commonOptions) {
    try {
        Path projectDir = commonOptions.project();
        Path pomFile = PomUtils.toPomFile(projectDir);
        Path projectConfigFile = ProjectConfig.toDotHelidon(projectDir);
        if (!Files.exists(projectConfigFile)) {
            // Find the helidon version if we can and create the config file
            Model model = PomUtils.readPomModel(pomFile);
            Parent parent = model.getParent();
            String helidonVersion = null;
            if (parent != null && parent.getGroupId().startsWith("io.helidon.")) {
                helidonVersion = parent.getVersion();
            }
            ensureProjectConfig(projectDir, helidonVersion);
        }
    } catch (IllegalArgumentException e) {
        String message = e.getMessage();
        if (message.contains("does not exist")) {
            message = NOT_A_PROJECT_DIR;
        }
        Requirements.failed(message);
    }
}
 
Example 2
Source File: QuarkusJsonPlatformDescriptorResolver.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static String getVersion(Model model) {
    if (model == null) {
        return null;
    }
    String version = model.getVersion();
    if (version != null) {
        return version;
    }
    final Parent parent = model.getParent();
    if (parent != null) {
        version = parent.getVersion();
        if (version != null) {
            return version;
        }
    }
    throw new IllegalStateException("Failed to determine the version for the POM of " + model.getArtifactId());
}
 
Example 3
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 4
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 5
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 6
Source File: GAV.java    From maven-git-versioning-extension with MIT License 6 votes vote down vote up
public static GAV of(Model model) {

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

        Parent parent = model.getParent();
        if (parent != null) {
            if (groupId == null) {
                groupId = parent.getGroupId();
            }
            if (version == null) {
                version = parent.getVersion();
            }
        }

        return new GAV(groupId, artifactId, version);
    }
 
Example 7
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 8
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 9
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 10
Source File: ModelUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the raw version of the model. If the model does not include
 * the version directly, it will return the version of the parent.
 * The version is raw in a sense if it's a property expression, the
 * expression will not be resolved.
 *
 * @param model POM
 * @return raw model
 */
public static String getRawVersion(Model model) {
    String version = model.getVersion();
    if (version != null) {
        return version;
    }
    final Parent parent = model.getParent();
    if (parent != null) {
        version = parent.getVersion();
        if (version != null) {
            return version;
        }
    }
    throw new IllegalStateException("Failed to determine version for project model");
}
 
Example 11
Source File: PomChangeParent.java    From butterfly with MIT License 5 votes vote down vote up
@Override
protected TOExecutionResult pomExecution(String relativePomFile, Model model) {
    String details;
    Parent parent = model.getParent();

    if (parent == null) {
        String message = String.format("Pom file %s does not have a parent", getRelativePath());

        switch (ifNotPresent) {
            case Warn:
                return TOExecutionResult.warning(this, new TransformationOperationException(message));
            case NoOp:
                return TOExecutionResult.noOp(this, message);
            case Fail:
                // Fail is the default
            default:
                return TOExecutionResult.error(this, new TransformationOperationException(message));
        }
    }

    if(groupId != null && artifactId != null && version != null) {
        parent.setGroupId(groupId);
        parent.setArtifactId(artifactId);
        parent.setVersion(version);
        String newParent = parent.toString();
        details = String.format("Parent for POM file (%s) has been set to %s", relativePomFile, newParent);
    } else if (groupId == null && artifactId == null && version != null) {
        String oldVersion = parent.getVersion();
        parent.setVersion(version);
        details = String.format("Parent's version for POM file (%s) has been changed from %s to %s", relativePomFile, oldVersion, version);
    } else {
        // FIXME this should be in a pre-validation
        return TOExecutionResult.error(this, new TransformationOperationException("Invalid POM parent transformation operation"));
    }

    return TOExecutionResult.success(this, details);
}
 
Example 12
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 13
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);
    }