From ef3a6bca8cceccf35c34bc3405e9aae99352b35f Mon Sep 17 00:00:00 2001 From: John Patrick <142304+nhojpatrick@users.noreply.github.com> Date: Mon, 27 Jul 2020 16:42:52 +0100 Subject: [PATCH] Inherit Global Settings and User Settings --- .../plugin/gitflow/AbstractGitFlowMojo.java | 384 +++++++++--------- 1 file changed, 186 insertions(+), 198 deletions(-) diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java index ee1756fc..492ba19c 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -15,6 +15,7 @@ */ package com.amashchenko.maven.plugin.gitflow; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -40,38 +41,55 @@ /** * Abstract git flow mojo. - * */ public abstract class AbstractGitFlowMojo extends AbstractMojo { - /** A full name of the versions-maven-plugin set goal. */ + /** + * A full name of the versions-maven-plugin set goal. + */ private static final String VERSIONS_MAVEN_PLUGIN_SET_GOAL = "org.codehaus.mojo:versions-maven-plugin:set"; - /** A full name of the versions-maven-plugin set-property goal. */ + /** + * A full name of the versions-maven-plugin set-property goal. + */ private static final String VERSIONS_MAVEN_PLUGIN_SET_PROPERTY_GOAL = "org.codehaus.mojo:versions-maven-plugin:set-property"; - /** Name of the tycho-versions-plugin set-version goal. */ + /** + * Name of the tycho-versions-plugin set-version goal. + */ private static final String TYCHO_VERSIONS_PLUGIN_SET_GOAL = "org.eclipse.tycho:tycho-versions-plugin:set-version"; - /** System line separator. */ + /** + * System line separator. + */ protected static final String LS = System.getProperty("line.separator"); - /** Success exit code. */ + /** + * Success exit code. + */ private static final int SUCCESS_EXIT_CODE = 0; - /** Pattern of disallowed characters in Maven commands. */ + /** + * Pattern of disallowed characters in Maven commands. + */ private static final Pattern MAVEN_DISALLOWED_PATTERN = Pattern .compile("[&|;]"); - /** Command line for Git executable. */ + /** + * Command line for Git executable. + */ private final Commandline cmdGit = new Commandline(); - /** Command line for Maven executable. */ + /** + * Command line for Maven executable. + */ private final Commandline cmdMvn = new Commandline(); - /** Git flow configuration. */ + /** + * Git flow configuration. + */ @Parameter(defaultValue = "${gitFlowConfig}") protected GitFlowConfig gitFlowConfig; /** * Git commit messages. - * + * * @since 1.2.1 */ @Parameter(defaultValue = "${commitMessages}") @@ -79,15 +97,15 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether this is Tycho build. - * + * * @since 1.1.0 */ @Parameter(defaultValue = "false") protected boolean tychoBuild; - + /** * Whether to call Maven install goal during the mojo execution. - * + * * @since 1.0.5 */ @Parameter(property = "installProject", defaultValue = "false") @@ -95,7 +113,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether to fetch remote branch and compare it with the local one. - * + * * @since 1.3.0 */ @Parameter(property = "fetchRemote", defaultValue = "true") @@ -103,7 +121,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether to print commands output into the console. - * + * * @since 1.0.7 */ @Parameter(property = "verbose", defaultValue = "false") @@ -111,7 +129,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Command line arguments to pass to the underlying Maven commands. - * + * * @since 1.8.0 */ @Parameter(property = "argLine") @@ -119,7 +137,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether to make a GPG-signed commit. - * + * * @since 1.9.0 */ @Parameter(property = "gpgSignCommit", defaultValue = "false") @@ -128,7 +146,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether to set -DgroupId='*' -DartifactId='*' when calling * versions-maven-plugin. - * + * * @since 1.10.0 */ @Parameter(property = "versionsForceUpdate", defaultValue = "false") @@ -145,7 +163,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Whether to skip updating version. Useful with {@link #versionProperty} to be * able to update <code>revision</code> property without modifying version tag. - * + * * @since 1.13.0 */ @Parameter(property = "skipUpdateVersion") @@ -153,7 +171,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { /** * Prefix that is applied to commit messages. - * + * * @since 1.14.0 */ @Parameter(property = "commitMessagePrefix") @@ -170,23 +188,28 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { @Parameter(property = "gitExecutable") private String gitExecutable; - /** Maven session. */ + /** + * Maven session. + */ @Parameter(defaultValue = "${session}", readonly = true) protected MavenSession mavenSession; @Component protected ProjectBuilder projectBuilder; - - /** Default prompter. */ + + /** + * Default prompter. + */ @Component protected Prompter prompter; - /** Maven settings. */ + /** + * Maven settings. + */ @Parameter(defaultValue = "${settings}", readonly = true) protected Settings settings; /** * Initializes command line executables. - * */ private void initExecutables() { if (StringUtils.isBlank(cmdMvn.getExecutable())) { @@ -206,11 +229,9 @@ private void initExecutables() { /** * Validates plugin configuration. Throws exception if configuration is not * valid. - * - * @param params - * Configuration parameters to validate. - * @throws MojoFailureException - * If configuration is not valid. + * + * @param params Configuration parameters to validate. + * @throws MojoFailureException If configuration is not valid. */ protected void validateConfiguration(String... params) throws MojoFailureException { @@ -232,7 +253,7 @@ protected void validateConfiguration(String... params) /** * Gets current project version from pom.xml file. - * + * * @return Current project version. * @throws MojoFailureException */ @@ -247,15 +268,15 @@ protected String getCurrentProjectVersion() throws MojoFailureException { /** * Reloads project info from file - * + * * @param project * @return * @throws MojoFailureException */ private MavenProject reloadProject(MavenProject project) throws MojoFailureException { try { - ProjectBuildingResult result = projectBuilder.build(project.getFile(), mavenSession.getProjectBuildingRequest()); - return result.getProject(); + ProjectBuildingResult result = projectBuilder.build(project.getFile(), mavenSession.getProjectBuildingRequest()); + return result.getProject(); } catch (Exception e) { throw new MojoFailureException("Error re-loading project info", e); } @@ -263,9 +284,9 @@ private MavenProject reloadProject(MavenProject project) throws MojoFailureExcep /** * Compares the production branch name with the development branch name. - * + * * @return <code>true</code> if the production branch name is different from - * the development branch name, <code>false</code> otherwise. + * the development branch name, <code>false</code> otherwise. */ protected boolean notSameProdDevName() { return !gitFlowConfig.getProductionBranch().equals( @@ -274,7 +295,7 @@ protected boolean notSameProdDevName() { /** * Checks uncommitted changes. - * + * * @throws MojoFailureException * @throws CommandLineException */ @@ -319,11 +340,10 @@ protected void checkSnapshotDependencies() throws MojoFailureException { /** * Checks if branch name is acceptable. - * - * @param branchName - * Branch name to check. + * + * @param branchName Branch name to check. * @return <code>true</code> when name is valid, <code>false</code> - * otherwise. + * otherwise. * @throws MojoFailureException * @throws CommandLineException */ @@ -336,9 +356,9 @@ protected boolean validBranchName(final String branchName) /** * Executes git commands to check for uncommitted changes. - * + * * @return <code>true</code> when there are uncommitted changes, - * <code>false</code> otherwise. + * <code>false</code> otherwise. * @throws CommandLineException * @throws MojoFailureException */ @@ -378,7 +398,7 @@ private boolean executeGitHasUncommitted() throws MojoFailureException, /** * Executes git config commands to set Git Flow configuration. - * + * * @throws MojoFailureException * @throws CommandLineException */ @@ -405,11 +425,9 @@ protected void initGitFlowConfig() throws MojoFailureException, /** * Executes git config command. - * - * @param name - * Option name. - * @param value - * Option value. + * + * @param name Option name. + * @param value Option value. * @throws MojoFailureException * @throws CommandLineException */ @@ -425,11 +443,9 @@ private void gitSetConfig(final String name, String value) /** * Executes git for-each-ref with <code>refname:short</code> format. - * - * @param branchName - * Branch name to find. - * @param firstMatch - * Return first match. + * + * @param branchName Branch name to find. + * @param firstMatch Return first match. * @return Branch names which matches <code>refs/heads/{branchName}*</code>. * @throws MojoFailureException * @throws CommandLineException @@ -441,19 +457,16 @@ protected String gitFindBranches(final String branchName, final boolean firstMat /** * Executes git for-each-ref with <code>refname:short</code> format. - * - * @param refs - * Refs to search. - * @param branchName - * Branch name to find. - * @param firstMatch - * Return first match. + * + * @param refs Refs to search. + * @param branchName Branch name to find. + * @param firstMatch Return first match. * @return Branch names which matches <code>{refs}{branchName}*</code>. * @throws MojoFailureException * @throws CommandLineException */ private String gitFindBranches(final String refs, final String branchName, - final boolean firstMatch) throws MojoFailureException, + final boolean firstMatch) throws MojoFailureException, CommandLineException { String wildcard = "*"; if (branchName.endsWith("/")) { @@ -511,9 +524,8 @@ protected String gitFindLastTag() throws MojoFailureException, CommandLineExcept /** * Removes double quotes from the string. - * - * @param str - * String to remove quotes from. + * + * @param str String to remove quotes from. * @return String without quotes. */ private String removeQuotes(String str) { @@ -525,7 +537,7 @@ private String removeQuotes(String str) { /** * Gets the current branch name. - * + * * @return Current branch name. * @throws MojoFailureException * @throws CommandLineException @@ -539,10 +551,9 @@ protected String gitCurrentBranch() throws MojoFailureException, CommandLineExce /** * Checks if local branch with given name exists. * - * @param branchName - * Name of the branch to check. + * @param branchName Name of the branch to check. * @return <code>true</code> if local branch exists, <code>false</code> - * otherwise. + * otherwise. * @throws MojoFailureException * @throws CommandLineException */ @@ -556,8 +567,7 @@ protected boolean gitCheckBranchExists(final String branchName) /** * Checks if local tag with given name exists. * - * @param tagName - * Name of the tag to check. + * @param tagName Name of the tag to check. * @return <code>true</code> if local tag exists, <code>false</code> otherwise. * @throws MojoFailureException * @throws CommandLineException @@ -571,8 +581,7 @@ protected boolean gitCheckTagExists(final String tagName) throws MojoFailureExce /** * Executes git checkout. * - * @param branchName - * Branch name to checkout. + * @param branchName Branch name to checkout. * @throws MojoFailureException * @throws CommandLineException */ @@ -586,15 +595,13 @@ protected void gitCheckout(final String branchName) /** * Executes git checkout -b. * - * @param newBranchName - * Create branch with this name. - * @param fromBranchName - * Create branch from this branch. + * @param newBranchName Create branch with this name. + * @param fromBranchName Create branch from this branch. * @throws MojoFailureException * @throws CommandLineException */ protected void gitCreateAndCheckout(final String newBranchName, - final String fromBranchName) throws MojoFailureException, + final String fromBranchName) throws MojoFailureException, CommandLineException { getLog().info( "Creating a new branch '" + newBranchName + "' from '" @@ -606,10 +613,8 @@ protected void gitCreateAndCheckout(final String newBranchName, /** * Executes git branch. * - * @param newBranchName - * Create branch with this name. - * @param fromBranchName - * Create branch from this branch. + * @param newBranchName Create branch with this name. + * @param fromBranchName Create branch from this branch. * @throws MojoFailureException * @throws CommandLineException */ @@ -624,11 +629,10 @@ protected void gitCreateBranch(final String newBranchName, final String fromBran /** * Replaces properties in message. - * + * * @param message - * @param map - * Key is a string to replace wrapped in <code>@{...}</code>. Value - * is a string to replace with. + * @param map Key is a string to replace wrapped in <code>@{...}</code>. Value + * is a string to replace with. * @return */ private String replaceProperties(String message, Map<String, String> map) { @@ -642,9 +646,8 @@ private String replaceProperties(String message, Map<String, String> map) { /** * Executes git commit -a -m. - * - * @param message - * Commit message. + * + * @param message Commit message. * @throws MojoFailureException * @throws CommandLineException */ @@ -656,11 +659,9 @@ protected void gitCommit(final String message) throws MojoFailureException, /** * Executes git commit -a -m, replacing <code>@{map.key}</code> with * <code>map.value</code>. - * - * @param message - * Commit message. - * @param messageProperties - * Properties to replace in message. + * + * @param message Commit message. + * @param messageProperties Properties to replace in message. * @throws MojoFailureException * @throws CommandLineException */ @@ -685,24 +686,18 @@ protected void gitCommit(String message, Map<String, String> messageProperties) /** * Executes git rebase or git merge --ff-only or git merge --no-ff or git merge. - * - * @param branchName - * Branch name to merge. - * @param rebase - * Do rebase. - * @param noff - * Merge with --no-ff. - * @param ffonly - * Merge with --ff-only. - * @param message - * Merge commit message. - * @param messageProperties - * Properties to replace in message. + * + * @param branchName Branch name to merge. + * @param rebase Do rebase. + * @param noff Merge with --no-ff. + * @param ffonly Merge with --ff-only. + * @param message Merge commit message. + * @param messageProperties Properties to replace in message. * @throws MojoFailureException * @throws CommandLineException */ protected void gitMerge(final String branchName, boolean rebase, boolean noff, boolean ffonly, String message, - Map<String, String> messageProperties) + Map<String, String> messageProperties) throws MojoFailureException, CommandLineException { String sign = ""; if (gpgSignCommit) { @@ -735,27 +730,23 @@ protected void gitMerge(final String branchName, boolean rebase, boolean noff, b /** * Executes git merge --no-ff. - * - * @param branchName - * Branch name to merge. - * @param message - * Merge commit message. - * @param messageProperties - * Properties to replace in message. + * + * @param branchName Branch name to merge. + * @param message Merge commit message. + * @param messageProperties Properties to replace in message. * @throws MojoFailureException * @throws CommandLineException */ protected void gitMergeNoff(final String branchName, final String message, - final Map<String, String> messageProperties) + final Map<String, String> messageProperties) throws MojoFailureException, CommandLineException { gitMerge(branchName, false, true, false, message, messageProperties); } /** * Executes git merge --squash. - * - * @param branchName - * Branch name to merge. + * + * @param branchName Branch name to merge. * @throws MojoFailureException * @throws CommandLineException */ @@ -767,15 +758,11 @@ protected void gitMergeSquash(final String branchName) /** * Executes git tag -a [-s] -m. - * - * @param tagName - * Name of the tag. - * @param message - * Tag message. - * @param gpgSignTag - * Make a GPG-signed tag. - * @param messageProperties - * Properties to replace in message. + * + * @param tagName Name of the tag. + * @param message Tag message. + * @param gpgSignTag Make a GPG-signed tag. + * @param messageProperties Properties to replace in message. * @throws MojoFailureException * @throws CommandLineException */ @@ -796,9 +783,8 @@ protected void gitTag(final String tagName, String message, boolean gpgSignTag, /** * Executes git branch -d. - * - * @param branchName - * Branch name to delete. + * + * @param branchName Branch name to delete. * @throws MojoFailureException * @throws CommandLineException */ @@ -811,9 +797,8 @@ protected void gitBranchDelete(final String branchName) /** * Executes git branch -D. - * - * @param branchName - * Branch name to delete. + * + * @param branchName Branch name to delete. * @throws MojoFailureException * @throws CommandLineException */ @@ -826,9 +811,8 @@ protected void gitBranchDeleteForce(final String branchName) /** * Fetches and checkouts from remote if local branch doesn't exist. - * - * @param branchName - * Branch name to check. + * + * @param branchName Branch name to check. * @throws MojoFailureException * @throws CommandLineException */ @@ -848,9 +832,8 @@ protected void gitFetchRemoteAndCreate(final String branchName) /** * Executes git fetch and compares local branch with the remote. - * - * @param branchName - * Branch name to fetch and compare. + * + * @param branchName Branch name to fetch and compare. * @throws MojoFailureException * @throws CommandLineException */ @@ -882,28 +865,25 @@ protected void gitFetchRemoteAndCompare(final String branchName) /** * Executes git fetch and git for-each-ref with <code>refname:short</code> * format. Searches <code>refs/remotes/{remoteName}/</code>. - * - * @param remoteName - * Name of the remote. - * @param branchName - * Branch name to find. - * @param firstMatch - * Return first match. + * + * @param remoteName Name of the remote. + * @param branchName Branch name to find. + * @param firstMatch Return first match. * @return Branch names which matches <code>refs/heads/{branchName}*</code>. * @throws MojoFailureException * @throws CommandLineException */ protected String gitFetchAndFindRemoteBranches(final String remoteName, final String branchName, - final boolean firstMatch) throws MojoFailureException, CommandLineException { + final boolean firstMatch) throws MojoFailureException, CommandLineException { gitFetchRemote(); return gitFindBranches("refs/remotes/" + remoteName + "/", branchName, firstMatch); } /** * Executes git fetch. - * + * * @return <code>true</code> if git fetch returned success exit code, - * <code>false</code> otherwise. + * <code>false</code> otherwise. * @throws MojoFailureException * @throws CommandLineException */ @@ -913,11 +893,10 @@ private boolean gitFetchRemote() throws MojoFailureException, CommandLineExcepti /** * Executes git fetch with specific branch. - * - * @param branchName - * Branch name to fetch. + * + * @param branchName Branch name to fetch. * @return <code>true</code> if git fetch returned success exit code, - * <code>false</code> otherwise. + * <code>false</code> otherwise. * @throws MojoFailureException * @throws CommandLineException */ @@ -946,12 +925,10 @@ private boolean gitFetchRemote(final String branchName) /** * Executes git push, optionally with the <code>--follow-tags</code> * argument. - * - * @param branchName - * Branch name to push. - * @param pushTags - * If <code>true</code> adds <code>--follow-tags</code> argument - * to the git <code>push</code> command. + * + * @param branchName Branch name to push. + * @param pushTags If <code>true</code> adds <code>--follow-tags</code> argument + * to the git <code>push</code> command. * @throws MojoFailureException * @throws CommandLineException */ @@ -990,9 +967,8 @@ protected void gitPushDelete(final String branchName) /** * Executes 'set' goal of versions-maven-plugin or 'set-version' of * tycho-versions-plugin in case it is tycho build. - * - * @param version - * New version to set. + * + * @param version New version to set. * @throws MojoFailureException * @throws CommandLineException */ @@ -1031,7 +1007,7 @@ protected void mvnSetVersions(final String version) throws MojoFailureException, /** * Executes mvn clean test. - * + * * @throws MojoFailureException * @throws CommandLineException */ @@ -1047,7 +1023,7 @@ protected void mvnCleanTest() throws MojoFailureException, /** * Executes mvn clean install. - * + * * @throws MojoFailureException * @throws CommandLineException */ @@ -1060,9 +1036,8 @@ protected void mvnCleanInstall() throws MojoFailureException, /** * Executes Maven goals. - * - * @param goals - * The goals to execute. + * + * @param goals The goals to execute. * @throws Exception */ protected void mvnRun(final String goals) throws Exception { @@ -1073,9 +1048,8 @@ protected void mvnRun(final String goals) throws Exception { /** * Executes Git command and returns output. - * - * @param args - * Git command line arguments. + * + * @param args Git command line arguments. * @return Command output. * @throws CommandLineException * @throws MojoFailureException @@ -1087,9 +1061,8 @@ private String executeGitCommandReturn(final String... args) /** * Executes Git command without failing on non successful exit code. - * - * @param args - * Git command line arguments. + * + * @param args Git command line arguments. * @return Command result. * @throws CommandLineException * @throws MojoFailureException @@ -1101,9 +1074,8 @@ private CommandResult executeGitCommandExitCode(final String... args) /** * Executes Git command. - * - * @param args - * Git command line arguments. + * + * @param args Git command line arguments. * @throws CommandLineException * @throws MojoFailureException */ @@ -1114,9 +1086,8 @@ private void executeGitCommand(final String... args) /** * Executes Maven command. - * - * @param args - * Maven command line arguments. + * + * @param args Maven command line arguments. * @throws CommandLineException * @throws MojoFailureException */ @@ -1127,25 +1098,20 @@ private void executeMvnCommand(final String... args) /** * Executes command line. - * - * @param cmd - * Command line. - * @param failOnError - * Whether to throw exception on NOT success exit code. - * @param argStr - * Command line arguments as a string. - * @param args - * Command line arguments. + * + * @param cmd Command line. + * @param failOnError Whether to throw exception on NOT success exit code. + * @param argStr Command line arguments as a string. + * @param args Command line arguments. * @return {@link CommandResult} instance holding command exit code, output - * and error if any. + * and error if any. * @throws CommandLineException - * @throws MojoFailureException - * If <code>failOnError</code> is <code>true</code> and command - * exit code is NOT equals to 0. + * @throws MojoFailureException If <code>failOnError</code> is <code>true</code> and command + * exit code is NOT equals to 0. */ private CommandResult executeCommand(final Commandline cmd, - final boolean failOnError, final String argStr, - final String... args) throws CommandLineException, + final boolean failOnError, final String argStr, + final String... args) throws CommandLineException, MojoFailureException { // initialize executables initExecutables(); @@ -1159,10 +1125,32 @@ private CommandResult executeCommand(final Commandline cmd, cmd.clearArgs(); cmd.addArguments(args); + final String globalSettingsFileArgLine = String.format("-gs %s", this.mavenSession.getRequest() + .getGlobalSettingsFile() + .getAbsolutePath()); + + final String userSettingsFileArgLine = String.format("-s %s", this.mavenSession.getRequest() + .getUserSettingsFile() + .getAbsolutePath()); + + final StringBuilder argLineStr = new StringBuilder(); + if (StringUtils.isNotBlank(argStr)) { - cmd.createArg().setLine(argStr); + argLineStr.append(globalSettingsFileArgLine); + argLineStr.append(userSettingsFileArgLine); + + } else { + if (!argStr.contains("-gs")) { + argLineStr.append(globalSettingsFileArgLine); + } + if (!argStr.contains("-s")) { + argLineStr.append(userSettingsFileArgLine); + } + argLineStr.append(argStr); } + cmd.createArg().setLine(argLineStr.toString()); + final StringBufferStreamConsumer out = new StringBufferStreamConsumer( verbose); @@ -1192,7 +1180,7 @@ private static class CommandResult { private final String error; private CommandResult(final int exitCode, final String out, - final String error) { + final String error) { this.exitCode = exitCode; this.out = out; this.error = error;