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

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

From source file:org.jasig.maven.notice.AbstractNoticeMojo.java

License:Apache License

/**
 * Check if a project is excluded based on its artifactId or a parent's artifactId
 *///from   w w w .j  av  a2s.co  m
protected boolean isExcluded(MavenProject mavenProject, String rootArtifactId) {
    final Log logger = this.getLog();

    final String artifactId = mavenProject.getArtifactId();
    if (this.excludedModules.contains(artifactId)) {
        logger.info("Skipping aggregation of child module " + mavenProject.getName()
                + " with excluded artifactId: " + artifactId);
        return true;
    }

    MavenProject parentProject = mavenProject.getParent();
    while (parentProject != null && !rootArtifactId.equals(parentProject.getArtifactId())) {
        final String parentArtifactId = parentProject.getArtifactId();
        if (this.excludedModules.contains(parentArtifactId)) {
            logger.info("Skipping aggregation of child module " + mavenProject.getName()
                    + " with excluded parent artifactId: " + parentArtifactId);
            return true;
        }
        parentProject = parentProject.getParent();
    }

    return false;
}

From source file:org.jasig.maven.notice.LicenseResolvingNodeVisitor.java

License:Apache License

public boolean visit(DependencyNode node) {
    if (DependencyNode.INCLUDED == node.getState()) {
        final Artifact artifact = node.getArtifact();

        //Only resolve an artifact once, if already visited just skip it
        if (!visitedArtifacts.add(artifact)) {
            return true;
        }//ww w  .j  a  v a  2s.c  o m

        String name = null;
        String licenseName = null;

        //Look for a matching mapping first
        final ResolvedLicense resolvedLicense = this.loadLicenseMapping(artifact);
        if (resolvedLicense != null && resolvedLicense.getVersionType() != null) {
            final ArtifactLicense artifactLicense = resolvedLicense.getArtifactLicense();
            name = StringUtils.trimToNull(artifactLicense.getName());
            licenseName = StringUtils.trimToNull(artifactLicense.getLicense());
        }

        //If name or license are still null try loading from the project
        if (name == null || licenseName == null) {
            final MavenProject artifactProject = this.loadProject(artifact);
            if (artifactProject != null) {
                if (name == null) {
                    name = artifactProject.getName();
                }

                if (licenseName == null) {
                    final Model model = artifactProject.getModel();
                    final List<License> licenses = model.getLicenses();

                    if (licenses.size() == 1) {
                        licenseName = licenses.get(0).getName();
                    } else if (licenses.size() > 1) {
                        final StringBuilder licenseNameBuilder = new StringBuilder();
                        for (final Iterator<License> licenseItr = licenses.iterator(); licenseItr.hasNext();) {
                            final License license = licenseItr.next();
                            licenseNameBuilder.append(license.getName());
                            if (licenseItr.hasNext()) {
                                licenseNameBuilder.append(" or ");
                            }
                        }
                        licenseName = licenseNameBuilder.toString();
                    }
                }
            }
        }

        //Try fall-back match for name & license, hitting this implies the resolved license was an all-versions match
        if (resolvedLicense != null && (licenseName == null || name == null)) {
            final ArtifactLicense artifactLicense = resolvedLicense.getArtifactLicense();
            if (name == null) {
                name = StringUtils.trimToNull(artifactLicense.getName());
            }
            if (licenseName == null) {
                if (artifactLicense != null) {
                    licenseName = StringUtils.trimToNull(artifactLicense.getLicense());
                }
            }
        }

        //If no name is found fall back to groupId:artifactId
        if (name == null) {
            name = artifact.getGroupId() + ":" + artifact.getArtifactId();
        }

        //Record the artifact resolution outcome
        if (licenseName == null) {
            this.unresolvedArtifacts.add(artifact);
        } else {
            this.resolvedLicenses.put(name, licenseName);
        }
    }
    return true;
}

From source file:org.javagems.core.maven.DebianMojo.java

License:Apache License

/**
 * Copy properties from the maven project to the ant project.
 *
 * @param mavenProject/*from  ww w  .  ja  v a  2s.  c  om*/
 * @param antProject
 */
public void copyProperties(MavenProject mavenProject, Project antProject) {
    Properties mavenProps = mavenProject.getProperties();
    Iterator iter = mavenProps.keySet().iterator();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        antProject.setProperty(key, mavenProps.getProperty(key));
    }

    // Set the POM file as the ant.file for the tasks run directly in Maven.
    antProject.setProperty("ant.file", mavenProject.getFile().getAbsolutePath());

    // Add some of the common maven properties
    getLog().debug("Setting properties with prefix: " + propertyPrefix);
    antProject.setProperty((propertyPrefix + "project.groupId"), mavenProject.getGroupId());
    antProject.setProperty((propertyPrefix + "project.artifactId"), mavenProject.getArtifactId());
    antProject.setProperty((propertyPrefix + "project.name"), mavenProject.getName());
    if (mavenProject.getDescription() != null) {
        antProject.setProperty((propertyPrefix + "project.description"), mavenProject.getDescription());
    }
    antProject.setProperty((propertyPrefix + "project.version"), mavenProject.getVersion());
    antProject.setProperty((propertyPrefix + "project.packaging"), mavenProject.getPackaging());
    antProject.setProperty((propertyPrefix + "project.build.directory"),
            mavenProject.getBuild().getDirectory());
    antProject.setProperty((propertyPrefix + "project.build.outputDirectory"),
            mavenProject.getBuild().getOutputDirectory());
    antProject.setProperty((propertyPrefix + "project.build.testOutputDirectory"),
            mavenProject.getBuild().getTestOutputDirectory());
    antProject.setProperty((propertyPrefix + "project.build.sourceDirectory"),
            mavenProject.getBuild().getSourceDirectory());
    antProject.setProperty((propertyPrefix + "project.build.testSourceDirectory"),
            mavenProject.getBuild().getTestSourceDirectory());
    antProject.setProperty((propertyPrefix + "localRepository"), localRepository.toString());
    antProject.setProperty((propertyPrefix + "settings.localRepository"), localRepository.getBasedir());

    // Add properties for depenedency artifacts
    Set depArtifacts = mavenProject.getArtifacts();
    for (Iterator it = depArtifacts.iterator(); it.hasNext();) {
        Artifact artifact = (Artifact) it.next();

        String propName = artifact.getDependencyConflictId();

        antProject.setProperty(propertyPrefix + propName, artifact.getFile().getPath());
    }

    // Add a property containing the list of versions for the mapper
    StringBuffer versionsBuffer = new StringBuffer();
    for (Iterator it = depArtifacts.iterator(); it.hasNext();) {
        Artifact artifact = (Artifact) it.next();

        versionsBuffer.append(artifact.getVersion() + File.pathSeparator);
    }
    antProject.setProperty(versionsPropertyName, versionsBuffer.toString());
}

From source file:org.jboss.datavirt.commons.maven.plugin.GenerateFeaturesXmlMojo.java

License:Apache License

/**
 * Format the given artifact as a bundle string with the appropriate syntax
 * used by the karaf features.xml file. For example:
 * /* w  w w.ja va  2  s  .c  om*/
 * mvn:commons-configuration/commons-configuration/1.6
 * 
 * @param artifact
 */
private String formatArtifactAsBundle(Artifact artifact) throws Exception {
    StringBuilder builder = new StringBuilder();
    // If it's a bundle already, awesome.  If not, we need to wrap it
    // and include some useful meta-data.
    if (isBundle(artifact)) {
        // Example:  mvn:commons-configuration/commons-configuration/1.6
        builder.append("mvn:");
        builder.append(artifact.getGroupId());
        builder.append("/");
        builder.append(artifact.getArtifactId());
        builder.append("/");
        builder.append(artifact.getBaseVersion());
        if (!"jar".equalsIgnoreCase(artifact.getType())) {
            builder.append("/");
            builder.append(artifact.getType());
        }
    } else {
        // Example:  wrap:mvn:log4j/log4j/1.2.14$Bundle-SymbolicName=log4j.log4j&amp;Bundle-Version=1.2.14&amp;Bundle-Name=Log4j
        builder.append("wrap:mvn:");
        builder.append(artifact.getGroupId());
        builder.append("/");
        builder.append(artifact.getArtifactId());
        builder.append("/");
        builder.append(artifact.getBaseVersion());
        if (!"jar".equalsIgnoreCase(artifact.getType())) {
            builder.append("/");
            builder.append(artifact.getType());
        }

        MavenProject project = resolveProject(artifact);
        builder.append("$Bundle-SymbolicName=");
        builder.append(artifact.getGroupId());
        builder.append(".");
        builder.append(artifact.getArtifactId());
        builder.append("&Bundle-Version=");
        builder.append(sanitizeVersionForOsgi(artifact.getBaseVersion()));
        if (project.getName() != null && project.getName().trim().length() > 0) {
            builder.append("&Bundle-Name=");
            builder.append(project.getName());
        }
    }
    return builder.toString();
}

From source file:org.jboss.maven.plugins.enforcer.rules.version.OSGIVersionRule.java

License:Open Source License

/**
 * Entry point for OSGI version rule/*from   w  w w  . j  a v a2s.  c  o m*/
 *
 * @param helper
 *            the EnforcerRuleHelper
 * @throws EnforcerRuleException
 *             any exception during rule checking
 */

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
    MavenProject project = null;
    try {
        project = (MavenProject) helper.evaluate("${project}");
    } catch (ExpressionEvaluationException eee) {
        throw new EnforcerRuleException("Unable to retrieve the MavenProject: ", eee);
    }
    String version = project.getVersion();
    if (isSnapshort(version)) {
        helper.getLog().debug("Ignore SNAPSHOT version checking.");
    } else if (!isOSGIVersion(version, helper.getLog())) {
        throw new EnforcerRuleException(
                "Version of module " + project.getName() + ": [" + version + "] is not a valid OSGI version.");
    }
}

From source file:org.jboss.maven.plugins.qstools.checkers.PomNameChecker.java

License:Apache License

@Override
public void checkProject(MavenProject project, Document doc, Map<String, List<Violation>> results)
        throws Exception {
    Rules rules = getConfigurationProvider().getQuickstartsRules(project.getGroupId());
    String pattern = pomNameUtil.getExpectedPattern(project, rules);
    if (!pattern.equals(project.getName())) {
        Node nameNode = (Node) getxPath().evaluate("/project/name", doc, XPathConstants.NODE);
        int lineNumber = XMLUtil.getLineNumberFromNode(nameNode);
        String msg = "Project uses name [%s] but should use the define name: %s";
        addViolation(project.getFile(), results, lineNumber, String.format(msg, project.getName(), pattern));
    }//from w  ww.j  a v a2s  .  c o  m

}

From source file:org.jboss.maven.plugins.qstools.common.PomNameUtil.java

License:Apache License

public String getExpectedPattern(MavenProject project, Rules rules) throws IOException {
    String pomNamePattern = rules.getPomNamePattern();
    String pomNamePatternSubmodule = rules.getPomNamePatternForSubmodule();
    String folderName = project.getBasedir().getName();
    String parentFolder = project.getBasedir().getParentFile().getName();
    String pattern;//from  w ww .j  a v a2s.  c om
    if (projectUtil.isSubProjec(project)) {
        // Get Target Product from parent Readme
        File parentReadme = new File(project.getBasedir().getParent(), "README.md");
        String targetProject = getTargetProduct(parentReadme);
        pattern = pomNamePatternSubmodule.replace("<target-product>", targetProject)
                .replace("<project-folder>", parentFolder).replace("<submodule-folder>", folderName);
    } else {
        File readme = new File(project.getBasedir(), "README.md");
        if (readme.exists()) {
            String targetProject = getTargetProduct(readme);
            pattern = pomNamePattern.replace("<target-product>", targetProject).replace("<project-folder>",
                    folderName);
        } else {
            // Not able to get the targetProject. Using the existing name to avoid wrong violations
            pattern = project.getName();
        }
    }
    return pattern;
}

From source file:org.jboss.maven.plugins.qstools.fixers.PomNameFixer.java

License:Apache License

@Override
public void fixProject(MavenProject project, Document doc) throws Exception {
    Rules rules = getConfigurationProvider().getQuickstartsRules(project.getGroupId());
    String pattern = pomNameUtil.getExpectedPattern(project, rules);
    if (!pattern.equals(project.getName())) {
        Node nameNode = (Node) getxPath().evaluate("/project/name", doc, XPathConstants.NODE);
        if (nameNode == null) {
            nameNode = doc.createElement("name");
            Node projectNode = (Node) getxPath().evaluate("/project", doc, XPathConstants.NODE);
            projectNode.appendChild(doc.createTextNode("    "));
            projectNode.appendChild(nameNode);
        }/*  w ww.  j av a2 s . c  om*/
        nameNode.setTextContent(pattern);
        XMLUtil.writeXML(doc, project.getFile());
    }

}

From source file:org.jenkinsci.plugins.pipeline.maven.eventspy.handler.AbstractMavenEventHandler.java

License:Open Source License

public Xpp3Dom newElement(@Nonnull String name, @Nullable final MavenProject project) {
    Xpp3Dom projectElt = new Xpp3Dom(name);
    if (project == null) {
        return projectElt;
    }/*from w w w .j  ava 2s  .  co  m*/

    projectElt.setAttribute("name", project.getName());
    projectElt.setAttribute("groupId", project.getGroupId());
    projectElt.setAttribute("artifactId", project.getArtifactId());
    projectElt.setAttribute("version", project.getVersion());
    projectElt.setAttribute("packaging", project.getPackaging());

    if (project.getBasedir() != null) {
        try {
            projectElt.setAttribute("baseDir", project.getBasedir().getCanonicalPath());
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    }

    if (project.getFile() != null) {
        File projectFile = project.getFile();
        String absolutePath;
        try {
            absolutePath = projectFile.getCanonicalPath();
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }

        if (absolutePath.endsWith(File.separator + "pom.xml")
                || absolutePath.endsWith(File.separator + ".flattened-pom.xml")) {
            // JENKINS-43616: flatten-maven-plugin replaces the original pom as artifact with a .flattened-pom.xml
            // no tweak
        } else if (absolutePath.endsWith(File.separator + "dependency-reduced-pom.xml")) {
            // JENKINS-42302: maven-shade-plugin creates a temporary project file dependency-reduced-pom.xml
            // TODO see if there is a better way to implement this "workaround"
            absolutePath = absolutePath.replace(File.separator + "dependency-reduced-pom.xml",
                    File.separator + "pom.xml");
        } else if (absolutePath.endsWith(File.separator + ".git-versioned-pom.xml")) {
            // JENKINS-56666 maven-git-versioning-extension causes warnings due to temporary pom.xml file name '.git-versioned-pom.xml'
            // https://github.com/qoomon/maven-git-versioning-extension/blob/v4.1.0/src/main/java/me/qoomon/maven/gitversioning/VersioningMojo.java#L39
            // TODO see if there is a better way to implement this "workaround"
            absolutePath = absolutePath.replace(File.separator + ".git-versioned-pom.xml",
                    File.separator + "pom.xml");
        } else {
            String flattenedPomFilename = getMavenFlattenPluginFlattenedPomFilename(project);
            if (flattenedPomFilename == null) {
                logger.warn("[jenkins-event-spy] Unexpected Maven project file name '" + projectFile.getName()
                        + "', problems may occur");
            } else {
                if (absolutePath.endsWith(File.separator + flattenedPomFilename)) {
                    absolutePath = absolutePath.replace(File.separator + flattenedPomFilename,
                            File.separator + "pom.xml");
                } else {
                    logger.warn("[jenkins-event-spy] Unexpected Maven project file name '"
                            + projectFile.getName() + "', problems may occur");
                }
            }
        }
        projectElt.setAttribute("file", absolutePath);
    }

    Build build = project.getBuild();

    if (build != null) {
        Xpp3Dom buildElt = new Xpp3Dom("build");
        projectElt.addChild(buildElt);
        if (build.getOutputDirectory() != null) {
            buildElt.setAttribute("directory", build.getDirectory());
        }
        if (build.getSourceDirectory() != null) {
            buildElt.setAttribute("sourceDirectory", build.getSourceDirectory());
        }
    }

    return projectElt;
}

From source file:org.jenkinsci.plugins.pipeline.maven.eventspy.handler.ProjectStartedExecutionHandler.java

License:Open Source License

@Override
protected void addDetails(@Nonnull ExecutionEvent executionEvent, @Nonnull Xpp3Dom root) {
    super.addDetails(executionEvent, root);
    MavenProject parentProject = executionEvent.getProject().getParent();
    if (parentProject == null) {
        // nothing to do
    } else {//  ww  w .  ja va  2 s .  c o  m
        Xpp3Dom parentProjectElt = new Xpp3Dom("parentProject");
        root.addChild(parentProjectElt);
        parentProjectElt.setAttribute("name", parentProject.getName());
        parentProjectElt.setAttribute("groupId", parentProject.getGroupId());

        parentProjectElt.setAttribute("artifactId", parentProject.getArtifactId());
        parentProjectElt.setAttribute("version", parentProject.getVersion());
    }
}