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

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

Introduction

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

Prototype

public String getModelVersion() 

Source Link

Usage

From source file:com.rodiontsev.maven.plugins.buildinfo.providers.ProjectInfoProvider.java

License:Apache License

public Map<String, String> getInfo(MavenProject project, BuildInfoMojo mojo) {

    // finite set of project properties we expose
    final Map<String, String> props = new LinkedHashMap<String, String>(65);
    props.put("project.id", project.getId());
    props.put("project.groupId", project.getGroupId());
    props.put("project.artifactId", project.getArtifactId());
    props.put("project.version", project.getVersion());
    props.put("project.name", project.getName());
    props.put("project.description", project.getDescription());
    props.put("project.modelVersion", project.getModelVersion());
    props.put("project.inceptionYear", project.getInceptionYear());
    props.put("project.packaging", project.getPackaging());
    props.put("project.url", project.getUrl());
    final MavenProject parent = project.getParent();
    if (parent != null) {
        props.put("project.parent.id", parent.getId());
        props.put("project.parent.groupId", parent.getGroupId());
        props.put("project.parent.artifactId", parent.getArtifactId());
        props.put("project.parent.version", parent.getVersion());
        props.put("project.parent.name", parent.getName());
        props.put("project.parent.description", parent.getDescription());
        props.put("project.parent.modelVersion", parent.getModelVersion());
        props.put("project.parent.inceptionYear", parent.getInceptionYear());
        props.put("project.parent.packaging", parent.getPackaging());
        props.put("project.parent.url", parent.getUrl());
    }// ww w . ja va2  s  . co  m

    // properties the user wants
    Map<String, String> info = new LinkedHashMap<String, String>();

    for (String propertyName : mojo.getProjectProperties()) {
        String prop = props.get(propertyName);
        if (prop != null) {
            info.put(propertyName, prop);
        }
    }
    info.put("build.time", DateFormatUtils.format(new Date(), "d MMMM yyyy, HH:mm:ss ZZ", Locale.ENGLISH));

    return info;
}

From source file:com.rodiontsev.maven.plugins.buildinfo.providers.ProjectPropertiesProvider.java

License:Apache License

public Map<String, String> getInfo(MavenProject project, BuildInfoMojo mojo) {
    // finite set of project properties we expose
    final Map<String, String> projectProperties = new LinkedHashMap<String, String>(65);
    projectProperties.put("project.id", project.getId());
    projectProperties.put("project.groupId", project.getGroupId());
    projectProperties.put("project.artifactId", project.getArtifactId());
    projectProperties.put("project.version", project.getVersion());
    projectProperties.put("project.name", project.getName());
    projectProperties.put("project.description", project.getDescription());
    projectProperties.put("project.modelVersion", project.getModelVersion());
    projectProperties.put("project.inceptionYear", project.getInceptionYear());
    projectProperties.put("project.packaging", project.getPackaging());
    projectProperties.put("project.url", project.getUrl());

    MavenProject parent = project.getParent();
    if (parent != null) {
        projectProperties.put("project.parent.id", parent.getId());
        projectProperties.put("project.parent.groupId", parent.getGroupId());
        projectProperties.put("project.parent.artifactId", parent.getArtifactId());
        projectProperties.put("project.parent.version", parent.getVersion());
        projectProperties.put("project.parent.name", parent.getName());
        projectProperties.put("project.parent.description", parent.getDescription());
        projectProperties.put("project.parent.modelVersion", parent.getModelVersion());
        projectProperties.put("project.parent.inceptionYear", parent.getInceptionYear());
        projectProperties.put("project.parent.packaging", parent.getPackaging());
        projectProperties.put("project.parent.url", parent.getUrl());
    }/*from   ww  w  .j av  a 2  s.c o m*/

    Map<String, String> info = new LinkedHashMap<String, String>();

    new InfoWriter().write(info, mojo.getProjectProperties(), new PropertyMapper() {
        @Override
        public String mapProperty(String propertyName) {
            return projectProperties.get(propertyName);
        }
    });

    return info;
}

From source file:io.sundr.maven.GenerateBomMojo.java

License:Apache License

/**
 * Returns the generated {@link org.apache.maven.project.MavenProject} to build.
 * This version of the project contains all the stuff needed for building (parents, profiles, properties etc).
 *
 * @param project The source {@link org.apache.maven.project.MavenProject}.
 * @param config  The {@link io.sundr.maven.BomConfig}.
 * @return The build {@link org.apache.maven.project.MavenProject}.
 */// ww w .j  a  v  a2  s  . c  o m
private static MavenProject toBuild(MavenProject project, BomConfig config) {
    File outputDir = new File(project.getBuild().getOutputDirectory());
    File bomDir = new File(outputDir, config.getArtifactId());
    File generatedBom = new File(bomDir, BOM_NAME);

    MavenProject toBuild = project.clone();
    //we want to avoid recursive "generate-bom".
    toBuild.setExecutionRoot(false);
    toBuild.setFile(generatedBom);
    toBuild.getModel().setPomFile(generatedBom);
    toBuild.setModelVersion(project.getModelVersion());

    toBuild.setArtifact(new DefaultArtifact(project.getGroupId(), config.getArtifactId(), project.getVersion(),
            project.getArtifact().getScope(), project.getArtifact().getType(),
            project.getArtifact().getClassifier(), project.getArtifact().getArtifactHandler()));

    toBuild.setParent(project.getParent());
    toBuild.getModel().setParent(project.getModel().getParent());

    toBuild.setGroupId(project.getGroupId());
    toBuild.setArtifactId(config.getArtifactId());
    toBuild.setVersion(project.getVersion());
    toBuild.setPackaging("pom");
    toBuild.setName(config.getName());
    toBuild.setDescription(config.getDescription());

    toBuild.setUrl(project.getUrl());
    toBuild.setLicenses(project.getLicenses());
    toBuild.setScm(project.getScm());
    toBuild.setDevelopers(project.getDevelopers());
    toBuild.setDistributionManagement(project.getDistributionManagement());
    toBuild.getModel().setProfiles(project.getModel().getProfiles());

    //We want to avoid having the generated stuff wiped.
    toBuild.getProperties().put("clean.skip", "true");
    toBuild.getModel().getBuild().setDirectory(bomDir.getAbsolutePath());
    toBuild.getModel().getBuild().setOutputDirectory(new File(bomDir, "target").getAbsolutePath());
    for (String key : config.getProperties().stringPropertyNames()) {
        toBuild.getProperties().put(key, config.getProperties().getProperty(key));
    }
    return toBuild;
}

From source file:org.kloeckner.maven.plugin.VersionRange.java

License:Apache License

private void readWritePom(MavenProject project) throws MojoExecutionException {

    Document document;//from  ww w  .  j a v  a2 s  .co m
    String intro = null;
    String outtro = null;
    try {
        String content = VersionRangeUtils.readXmlFile(VersionRangeUtils.getStandardPom(project),
                VersionRangeUtils.LS);
        // we need to eliminate any extra whitespace inside elements, as
        // JDOM will nuke it
        content = content.replaceAll("<([^!][^>]*?)\\s{2,}([^>]*?)>", "<$1 $2>");
        content = content.replaceAll("(\\s{2,}|[^\\s])/>", "$1 />");

        SAXBuilder builder = new SAXBuilder();
        document = builder.build(new StringReader(content));

        // Normalize line endings to platform's style (XML processors like
        // JDOM normalize line endings to "\n" as
        // per section 2.11 of the XML spec)
        VersionRangeUtils.normaliseLineEndings(document);

        // rewrite DOM as a string to find differences, since text outside
        // the root element is not tracked
        StringWriter w = new StringWriter();
        Format format = Format.getRawFormat();
        format.setLineSeparator(VersionRangeUtils.LS);
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);

        int index = content.indexOf(w.toString());
        if (index >= 0) {
            intro = content.substring(0, index);
            outtro = content.substring(index + w.toString().length());
        } else {
            /*
             * NOTE: Due to whitespace, attribute reordering or entity
             * expansion the above indexOf test can easily fail. So let's
             * try harder. Maybe some day, when JDOM offers a StaxBuilder
             * and this builder employes
             * XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, this whole mess
             * can be avoided.
             */
            final String SPACE = "\\s++";
            final String XML = "<\\?(?:(?:[^\"'>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+>";
            final String INTSUB = "\\[(?:(?:[^\"'\\]]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+\\]";
            final String DOCTYPE = "<!DOCTYPE(?:(?:[^\"'\\[>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+')|(?:" + INTSUB
                    + "))*+>";
            final String PI = XML;
            final String COMMENT = "<!--(?:[^-]|(?:-[^-]))*+-->";

            final String INTRO = "(?:(?:" + SPACE + ")|(?:" + XML + ")|(?:" + DOCTYPE + ")|(?:" + COMMENT
                    + ")|(?:" + PI + "))*";
            final String OUTRO = "(?:(?:" + SPACE + ")|(?:" + COMMENT + ")|(?:" + PI + "))*";
            final String POM = "(?s)(" + INTRO + ")(.*?)(" + OUTRO + ")";

            Matcher matcher = Pattern.compile(POM).matcher(content);
            if (matcher.matches()) {
                intro = matcher.group(1);
                outtro = matcher.group(matcher.groupCount());
            }
        }
    } catch (JDOMException e) {
        throw new MojoExecutionException("Error reading POM: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Error reading POM: " + e.getMessage(), e);
    }

    List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
    Object result = new Object();
    transformDocument(project, document.getRootElement(), reactorProjects, result, false);

    // for overwriting:
    File pomFile = VersionRangeUtils.getStandardPom(project);
    // File pomFile = new File(project.getBasedir(), "newpom.xml");

    // if (simulate) {
    // File outputFile = new File(pomFile.getParentFile(), pomFile.getName()
    // + "." + pomSuffix);
    // writePom(outputFile, document, releaseDescriptor,
    // project.getModelVersion(), intro, outtro);
    // } else {

    VersionRangeUtils.writePom(pomFile, document, project.getModelVersion(), intro, outtro);
    // }

}