Example usage for org.apache.maven.project MavenProject getVersion

List of usage examples for org.apache.maven.project MavenProject getVersion

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getVersion.

Prototype

public String getVersion() 

Source Link

Usage

From source file:org.wso2.maven.pckg.prepare.PackagePrepareSystemScopeMojo.java

License:Open Source License

private void aggregateDependencies(List<MavenProject> mavenProjects) {
    dependencySystemPathMap = new HashMap<>();

    for (MavenProject mavenProject : mavenProjects) {
        String packaging = mavenProject.getPackaging();
        // CAPP projects are ignored.
        if (packaging == null || !MavenConstants.CAPP_PACKAGING.equals(packaging)) {
            try {
                dependencySystemPathMap.putAll(PackagePrepareUtils.getArtifactsSystemPathMap(mavenProject));
            } catch (FactoryConfigurationError | Exception e) {
                // Can proceed even if this is reached
                log.warn("Failed to retrieve dependencies from project: " + mavenProject.getGroupId() + ":"
                        + mavenProject.getArtifactId() + ":" + mavenProject.getVersion(), e);
            }//from w  w w .  j a  v  a  2  s.  co m
        }
    }

    if (isDebugEnabled) {
        Iterator<Entry<String, String>> dependencyIterator = dependencySystemPathMap.entrySet().iterator();
        while (dependencyIterator.hasNext()) {
            log.debug("Identified system path of: " + dependencyIterator.next().getKey());
        }
    }
}

From source file:org.wso2.maven.RollbackReleaseMojo.java

License:Open Source License

@Override
protected String getNewVersion(File artifactXml) throws IOException, XmlPullParserException {
    // Read the backup pom file created by maven-release-plugin.
    File releaseBackupPOM = new File(artifactXml.getParent() + File.separator + POM_XML + RELEASE_BACKUP_SFX);
    if (releaseBackupPOM.exists()) {
        MavenProject mavenProjectBackup = getMavenProject(releaseBackupPOM);
        return mavenProjectBackup.getVersion();
    } else {//from  w  w w  .j  a va2s.co  m
        log.error("Cannot find " + releaseBackupPOM.getPath()
                + " file. Make sure you have invoked this goal before invoking"
                + " release:rollback of maven-release-plugin.");
        return null;
    }
}

From source file:se.jguru.nazgul.tools.codestyle.enforcer.rules.AbstractEnforcerRule.java

License:Apache License

/**
 * This is the interface into the rule. This method should throw an exception
 * containing a reason message if the rule fails the check. The plugin will
 * then decide based on the fail flag if it should stop or just log the
 * message as a warning.//from   ww w .ja va  2  s .c  om
 *
 * @param helper The helper provides access to the log, MavenSession and has
 *               helpers to get common components. It is also able to lookup components
 *               by class name.
 * @throws org.apache.maven.enforcer.rule.api.EnforcerRuleException the enforcer rule exception
 */
@Override
@SuppressWarnings("PMD.PreserveStackTrace")
public final void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException {

    final MavenProject project;
    try {
        project = (MavenProject) helper.evaluate("${project}");
    } catch (final ExpressionEvaluationException e) {

        // Whoops.
        final String msg = "Could not acquire MavenProject. (Expression lookup failure for: "
                + e.getLocalizedMessage() + ")";
        throw new EnforcerRuleException(msg, e);
    }

    // Delegate.
    try {
        performValidation(project, helper);
    } catch (RuleFailureException e) {

        // Create a somewhat verbose failure message.
        String message = "\n" + "\n#" + "\n# Structure rule failure:" + "\n# " + getShortRuleDescription()
                + "\n# " + "\n# Message: " + e.getLocalizedMessage() + "\n# " + "\n# Offending project ["
                + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + "]"
                + "\n#";

        final Artifact art = e.getOffendingArtifact();
        if (art != null) {

            message += "\n# Offending artifact [" + art.getGroupId() + ":" + art.getArtifactId() + ":"
                    + art.getVersion() + "]" + "\n#";
        }
        message += "\n";

        // Re-throw for pretty print
        throw new EnforcerRuleException(message);
    }
}

From source file:se.jguru.nazgul.tools.codestyle.enforcer.rules.ProjectType.java

License:Apache License

/**
 * Acquires the ProjectType instance for the provided MavenProject,
 * or throws an IllegalArgumentException holding an exception message
 * if a ProjectType could not be found for the provided MavenProject.
 *
 * @param project The MavenProject to classify.
 * @return The corresponding ProjectType.
 * @throws IllegalArgumentException if the given project could not be mapped to a [single] ProjectType.
 *                                  The exception message holds
 *//*  w  ww  .  j av  a2s .  co  m*/
public static ProjectType getProjectType(final MavenProject project) throws IllegalArgumentException {

    Validate.notNull(project, "Cannot handle null project argument.");

    final List<ProjectType> matches = findCandidates(project.getGroupId(), project.getArtifactId(),
            project.getPackaging(), "Incorrect project type definition for [" + project.getGroupId() + " :: "
                    + project.getArtifactId() + " :: " + project.getVersion() + "]: ");

    // Validate the internal requirements for the two different pom projects.
    final ProjectType toReturn = matches.get(0);
    switch (toReturn) {
    case PARENT:
    case ASSEMBLY:
        // This project should not contain modules.
        if (project.getModules() != null && !project.getModules().isEmpty()) {
            throw new IllegalArgumentException(
                    ProjectType.PARENT + " projects may not contain module definitions. "
                            + "(Modules are reserved for reactor projects).");
        }
        break;

    case REACTOR:
        // This project not contain dependency definitions.
        final List<?> dependencies = project.getDependencies();
        if (dependencies != null && !dependencies.isEmpty()) {
            throw new IllegalArgumentException(
                    ProjectType.REACTOR + " projects may not contain dependency definitions."
                            + " (Dependencies should be defined within parent projects).");
        }

        final DependencyManagement dependencyManagement = project.getDependencyManagement();
        if (dependencyManagement != null) {

            // Dig out all dependency management-defined dependencies.
            final List<Dependency> templateDependencies = dependencyManagement.getDependencies();
            if (templateDependencies != null && !templateDependencies.isEmpty()) {
                throw new IllegalArgumentException(
                        ProjectType.REACTOR + " projects may not contain dependency [management] definitions."
                                + " (Dependencies should be defined within parent projects).");
            }
        }
        break;

    // No action should be taken for other project types.
    default:
        break;
    }

    // All done.
    return toReturn;
}

From source file:sh.isaac.convert.rf2.mojo.VerifyIbdfVersionFormat.java

License:Apache License

@Override
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
    try {//w ww .j  a  v a2s  .co  m
        // get the various expressions out of the helper.
        MavenProject project = (MavenProject) helper.evaluate("${project}");
        String artifactId = project.getArtifactId();
        String version = project.getVersion();
        String sourceDataVersion = project.getProperties().getProperty("sourceData.version");
        String sourceDataArtifactId = project.getProperties().getProperty("sourceData.artifactId");
        String loaderVersion = project.getProperties().getProperty("loader.version");

        if (!artifactId.equals("solor-parent")) {
            if (artifactId.endsWith(ARTIFACT_SUFFIX)) {
                if (!sourceDataArtifactId.endsWith(SOURCE_DATA_SUFFIX)) {
                    throw new EnforcerRuleException(
                            "To follow convention, the source data artifact id must end in: "
                                    + SOURCE_DATA_SUFFIX + " found: " + sourceDataArtifactId);
                }

                if (!version.startsWith(sourceDataVersion)) {
                    throw new EnforcerRuleException(
                            "To follow convention, the version must start with the source data version: "
                                    + sourceDataVersion + " found: " + version);
                }

                if (!version.contains("-loader-" + loaderVersion)) {
                    throw new EnforcerRuleException(
                            "To follow convention, the version must contain the loader version: "
                                    + loaderVersion + " found: " + version);
                }

                String constructedVersionStart = sourceDataVersion + "-loader-" + loaderVersion;

                if (!version.startsWith(constructedVersionStart)) {
                    throw new EnforcerRuleException("To follow convention, the version must start with: "
                            + constructedVersionStart + " found: " + version);
                }
            }
        }
    } catch (ExpressionEvaluationException e) {
        throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e);
    }
}

From source file:uk.ac.ox.oucs.plugins.DiscoverMojo.java

License:Apache License

protected void deployAndDependents(Set<Artifact> artifacts)
        throws MojoExecutionException, MojoFailureException {
    loadCachedImplentations();//from  w w w.  j a  v  a  2  s  .  co m
    try {
        toDeploy.addAll(artifacts);
        do {
            ArtifactResolutionResult arr = customResolver.resolveTransitively(artifacts, project.getArtifact(),
                    localRepository, remoteRepositories, customMetadataSource, null);
            Set<Artifact> resolvedArtifacts = arr.getArtifacts();

            Set<ResolutionNode> arrRes = arr.getArtifactResolutionNodes();

            for (ResolutionNode node : arrRes) {
                getLog().info(node.getArtifact().getArtifactId());
                for (String artifactId : (List<String>) node.getDependencyTrail()) {
                    getLog().info("  +" + artifactId);
                }
            }

            Set<Artifact> artifactsToFind = new HashSet<Artifact>();

            for (Artifact artifact : resolvedArtifacts) {
                if (needsImplementation(artifact)) {
                    getLog().debug("Needed : " + artifact.toString() + " " + artifact.getDependencyTrail());
                    artifactsToFind.add(artifact);
                } else {
                    getLog().debug("Ignored : " + artifact.toString() + " " + artifact.getDependencyTrail());
                }

            }

            artifacts = new HashSet<Artifact>();
            for (Artifact artifact : artifactsToFind) {
                String artifactKey = artifact.getGroupId() + ":" + artifact.getArtifactId();
                if (!checkedArtifacts.contains(artifactKey)) {
                    toDeploy.add(artifact);
                    MavenProject project = findImplementation(artifact);
                    if (project != null) {
                        getLog().info("Found implementation: " + artifactKey + " to " + project.getGroupId()
                                + ":" + project.getArtifactId() + ":" + project.getVersion());
                        Set<Artifact> projectArtifacts = project.createArtifacts(customArtifactFactory, null,
                                null);
                        //artifacts.addAll(projectArtifacts);
                        if (shouldExpand(project)) {

                            toDeploy.addAll(projectArtifacts);
                        }
                        artifacts.add(project.getArtifact());
                        toDeploy.add(project.getArtifact());

                    } else {
                        getLog().info("Unresolved implementation: " + artifactKey);

                    }
                    checkedArtifacts.add(artifactKey);
                }
            }
        } while (artifacts.size() > 0);
    } catch (InvalidDependencyVersionException e1) {
        throw new MojoExecutionException("Failed to create artifacts", e1);
    } catch (ArtifactResolutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ArtifactNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        saveCachedImplmentations();
    }

    addToDependencies();

}

From source file:uk.co.unclealex.executable.plugin.ExecutableMojo.java

License:Apache License

/**
 * Deletes file-sets in the following project build directory order: (source)
 * directory, output directory, test directory, report directory, and then the
 * additional file-sets./*  www  .j a  v  a 2 s. c o m*/
 * 
 * @throws MojoExecutionException
 *           When a directory failed to get deleted.
 * @see org.apache.maven.plugin.Mojo#execute()
 */
public void execute() throws MojoExecutionException {
    Scripter scripter = Guice.createInjector(new CodeGeneratorModule()).getInstance(Scripter.class);
    Path buildDirectory = getBuildDirectory().toPath();
    Path classesDirectory = getOutputDirectory().toPath();
    MavenProject project = getProject();
    String version = project.getVersion();
    Path targetPath = buildDirectory.resolve(project.getArtifactId() + "-" + version + ".sh");
    Path workDirectory = buildDirectory.resolve("executable");
    Iterable<Path> dependencyJarFiles = Iterables.transform(getClasspathElements(), new PathFunction());
    try {
        scripter.generate(targetPath, classesDirectory, getHomeExpression(), version, workDirectory,
                dependencyJarFiles);
        //getProjectHelper(). attachArtifact(project, "sh", targetPath.toFile());
        project.getArtifact().setFile(targetPath.toFile());
    } catch (IOException | ExecutableScanException e) {
        throw new MojoExecutionException("Building an executable script failed.", e);
    }
}