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

com.hbakkum.maven.plugins.ResolveParentVersionMojo Maven / Gradle / Ivy

Go to download

Maven plugin which resolves any property placeholders in the version field of a parent pom reference

There is a newer version: 0.8
Show newest version
package com.hbakkum.maven.plugins;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * This plugin updates a pom file with the resolved parent version. That is, it ensures that for a pom
 * that has a parent pom reference, that when it gets installed/deployed, any property placeholders in the parent
 * pom version field will be resolved.
 *
 * e.g.
 *
 * Say we have the following pom:
 *
 * <project>
 *   <parent>
 *     <groupId>com.mycompany.app</groupId>
 *     <artifactId>my-app</artifactId>
 *     <version>1.${some.property}</version>
 *  </parent>
 *
 *  <artifactId>my-module</artifactId>
 *
 *  ...
 *
 * </project>
 *
 *
 * If a build runs where some.property=999, then the installed/deployed pom will be:
 *
 * <project>
 *   <parent>
 *     <groupId>com.mycompany.app</groupId>
 *     <artifactId>my-app</artifactId>
 *     <version>1.999</version>
 *  </parent>
 *
 *  <artifactId>my-module</artifactId>
 *
 *  ...
 *
 * </project>
 *
 *
 **/
@Mojo(name = "resolve-parent-version", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true, requiresDirectInvocation = false, executionStrategy = "once-per-session")
public class ResolveParentVersionMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
    private File outputDirectory;

    @Parameter(property = "outputPomFilename", defaultValue = "resolved-parent-version-pom.xml")
    private String outputPomFilename;

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    public void execute() throws MojoExecutionException {
        MavenProject parentProject = project.getParent();
        if (parentProject == null) {
            getLog().warn("No parent project found - nothing to do");
            return;
        }

        Document doc = parsePomFile();

        Element parentVersionElement = getParentVersionElement(doc);
        if (parentVersionElement == null) {
            getLog().warn("Failed to find parent version element, doing nothing");
            return;
        }
        parentVersionElement.setTextContent(parentProject.getVersion());

        Path outputFile = outputDirectory.toPath().resolve(outputPomFilename);
        writeResolvedParentVersionPom(doc, outputFile);

        project.setPomFile(outputFile.toFile());
    }

    private Document parsePomFile() throws MojoExecutionException {
        File pomFile = project.getFile();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();

        } catch (ParserConfigurationException e) {
            throw new MojoExecutionException("Failed to create dom parser", e);
        }

        try {
            return db.parse(pomFile);

        } catch (SAXException e) {
            throw new MojoExecutionException("Failed to parse pom file", e);

        } catch (IOException e) {
            throw new MojoExecutionException("Failed to parse pom file", e);
        }
    }

    private Element getParentVersionElement(Document doc) throws MojoExecutionException {
        XPath xPath = XPathFactory.newInstance().newXPath();

        try {
            NodeList nodes = ((NodeList) xPath.evaluate("/project/parent/version", doc.getDocumentElement(), XPathConstants.NODESET));
            if (nodes.getLength() == 0) {
                return null;
            }

            return (Element) nodes.item(0);

        } catch (XPathExpressionException e) {
            throw new MojoExecutionException("Failed to evaluate xpath expression", e);
        }
    }

    private void writeResolvedParentVersionPom(Document doc, Path outputFile) throws MojoExecutionException {
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();

        } catch (TransformerConfigurationException e) {
            throw new MojoExecutionException("Failed to create xml transformer", e);
        }

        if (!outputDirectory.exists()) {
            try {
                Files.createDirectory(outputDirectory.toPath());

            } catch (IOException e) {
                throw new MojoExecutionException("Failed to create output directory", e);
            }
        }

        try {
            outputFile = Files.createFile(outputFile);

        } catch (IOException e) {
            throw new MojoExecutionException("Failed to create output file", e);
        }

        Result output = new StreamResult(outputFile.toFile());
        Source input = new DOMSource(doc);
        try {
            transformer.transform(input, output);

        } catch (TransformerException e) {
            throw new MojoExecutionException("Failed to write output file", e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy