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

org.apache.camel.maven.packaging.PrepareReleasePomMojo Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.camel.maven.packaging;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.camel.tooling.util.PackageHelper;
import org.apache.camel.tooling.util.Strings;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

/**
 * Prepares the apache-camel/pom.xml and common-bin to keep the Camel artifacts
 * up-to-date.
 */
@Mojo(name = "prepare-release-pom", threadSafe = true)
public class PrepareReleasePomMojo extends AbstractMojo {

    /**
     * The maven project.
     */
    @Parameter(property = "project", required = true, readonly = true)
    protected MavenProject project;

    /**
     * The apache-camel/pom
     */
    @Parameter(defaultValue = "${project.build.directory}/../../../apache-camel/pom.xml")
    protected File releasePom;

    /**
     * The apache-camel/descriptors/common-bin.xml
     */
    @Parameter(defaultValue = "${project.build.directory}/../../../apache-camel/src/main/descriptors/common-bin.xml")
    protected File commonBinXml;

    /**
     * The directory for components
     */
    @Parameter(defaultValue = "${project.build.directory}/../../../components")
    protected File componentsDir;

    /**
     * Maven ProjectHelper.
     */
    @Component
    private MavenProjectHelper projectHelper;

    /**
     * Execute goal.
     *
     * @throws MojoExecutionException execution of the main class or one of the
     *             threads it generated failed.
     * @throws MojoFailureException something bad happened...
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        updatePomAndCommonBin(componentsDir, "org.apache.camel", "camel components");
    }

    protected void updatePomAndCommonBin(File dir, String groupId, String token) throws MojoExecutionException, MojoFailureException {
        SortedSet artifactIds = new TreeSet<>();

        try {
            Set poms = new HashSet<>();
            findComponentPoms(dir, poms);
            for (File pom : poms) {
                String aid = asArtifactId(pom);
                if (isValidArtifactId(aid)) {
                    artifactIds.add(aid);
                }
            }
        } catch (IOException e) {
            throw new MojoFailureException("Error due " + e.getMessage(), e);
        }

        getLog().debug("ArtifactIds: " + artifactIds);

        // update pom.xml
        StringBuilder sb = new StringBuilder();
        for (String aid : artifactIds) {
            sb.append("    \n");
            sb.append("      " + groupId + "\n");
            sb.append("      " + aid + "\n");
            sb.append("      ${project.version}\n");
            sb.append("    \n");
        }
        String changed = sb.toString();
        boolean updated = updateXmlFile(releasePom, token, changed, "    ");

        if (updated) {
            getLog().info("Updated apache-camel/pom.xml file");
        } else {
            getLog().debug("No changes to apache-camel/pom.xml file");
        }
        getLog().info("apache-camel/pom.xml contains " + artifactIds.size() + " " + token + " dependencies");

        // update common-bin.xml
        sb = new StringBuilder();
        for (String aid : artifactIds) {
            sb.append("        " + groupId + ":" + aid + "\n");
        }
        changed = sb.toString();
        updated = updateXmlFile(commonBinXml, token, changed, "        ");

        if (updated) {
            getLog().info("Updated apache-camel/src/main/descriptors/common-bin.xml file");
        } else {
            getLog().debug("No changes to apache-camel/src/main/descriptors/common-bin.xml file");
        }
        getLog().info("apache-camel/src/main/descriptors/common-bin.xml contains " + artifactIds.size() + " " + token + " dependencies");
    }

    private void findComponentPoms(File parentDir, Set components) {
        File[] files = parentDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory() && file.getName().startsWith("camel-")) {
                    findComponentPoms(file, components);
                } else if (parentDir.getName().startsWith("camel-") && file.getName().equals("pom.xml")) {
                    components.add(file);
                }
            }
        }
    }

    private String asArtifactId(File pom) throws IOException {
        String text = PackageHelper.loadText(pom);
        text = Strings.after(text, "");
        if (text != null) {
            return Strings.between(text, "", "");
        }
        return null;
    }

    private boolean isValidArtifactId(String aid) {
        return aid != null && !aid.endsWith("-maven-plugin") && !aid.endsWith("-parent");
    }

    private boolean updateXmlFile(File file, String token, String changed, String spaces) throws MojoExecutionException {
        String start = "";
        String end = "";

        if (!file.exists()) {
            return false;
        }

        try {
            String text = PackageHelper.loadText(file);

            String existing = Strings.between(text, start, end);
            if (existing != null) {
                // remove leading line breaks etc
                existing = existing.trim();
                changed = changed.trim();
                if (existing.equals(changed)) {
                    return false;
                } else {
                    String before = Strings.before(text, start);
                    String after = Strings.after(text, end);
                    text = before + start + "\n" + spaces + changed + "\n" + spaces + end + after;
                    PackageHelper.writeText(file, text);
                    return true;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy