All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smartcodeltd.VersionMojo Maven / Gradle / Ivy

package com.smartcodeltd;

import com.smartcodeltd.domain.Version;
import com.smartcodeltd.writer.Writer;
import de.pdark.decentxml.Document;
import de.pdark.decentxml.Element;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.maven.plugins.annotations.LifecyclePhase.PACKAGE;

/**
 * Retrieves project version specified in pom.xml and outputs it
 * either to stdout or to a file, depending on configuration.
 */
@Mojo(name = "version", requiresProject = true, defaultPhase = PACKAGE, aggregator = true)
public class VersionMojo
    extends ReleaseCandidateMojo
{
    private final static String default_output_uri      = "stdout";
    private final static String default_output_template = "{{ version }}";

    /**
     * 

* Defines where to direct the output of release-candidate:version *

* *
    *
  • Setting outputUri to stdout will print output to the console,
  • *
  • using file://${project.basedir}/project.properties will direct the output to a project.properties file for example.
  • *
* *

* Please note that you should use an absolute path when specifying the outputUri. * You can get hold of your project base directory by using ${project.basedir} as per the example above. *

*/ @Parameter(defaultValue = default_output_uri, required = false, property = "outputUri") private URI outputUri; /** *

* Defines how to structure the output of release-candidate:version *

* *

* If your build server of choice understands text output produced by maven (which is the case if you're using * TeamCity for example), you can specify the outputTemplate as: *

* *
<outputTemplate>
     *  ##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']
     *  ##teamcity[message text='Project version: {{ version }}']
     * </outputTemplate>
* *

* If your build server prefers to use env variables defined using property files (Jenkins with EnvInject plugin) * you can specify the outputTemplate as: *

* *
<outputTemplate>PROJECT_VERSION={{ version }}</outputTemplate>
* *

* Please note that when using multi-line templates, leading whitespace characters will be stripped. *

*/ @Parameter(defaultValue = default_output_template, required = false, property = "outputTemplate") protected String outputTemplate; @Override public void execute() throws MojoExecutionException { File pom = project.getFile(); try { Version version = currentVersionFrom(project.getFile()); info("Detected version: '%s'", version); Writer.from(outputUri, charset). write(version.formattedWith(outputTemplate)); } catch (Exception e) { throw new MojoExecutionException(String.format("Couldn't read project version from '%s'.", pom.getPath()), e); } } private Version currentVersionFrom(File pom) throws IOException { Document doc = parsed(pom); return new Version(checkNotNull(firstExisting( projectVersion(doc), parentVersion(doc) )).getText()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy