Maven pom variables as command argument

2019-12-16 20:16发布

问题:

I want to be able to do something like:

mvn release:branch -DbranchName=${project.version}

I'm sure this won't work and I'm asking for help on how to achieve it. mvn release:branch require a branchName. I need the branchName on the command line argument taking the project version as value. My pom looks like this:

<profile>
          <id>branch</id>
          <activation>
            <property>
              <name>branchName</name>
            </property>
          </activation>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                  <tagNameFormat>v@{project.version}-beta</tagNameFormat>
                  <autoVersionSubmodules>true</autoVersionSubmodules>
                  <suppressCommitBeforeBranch>true</suppressCommitBeforeBranch>
                  <remoteTagging>false</remoteTagging>
                </configuration>
              </plugin>
            </plugins>
          </build>
      </profile>

This profile is triggerred only when branchName is present on the command line argument.

I appreciate your help.

Edit: updated pom

<profile>
          <id>branch</id>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                  <branchName>@{project.version}</branchName>
                  <tagNameFormat>v@{project.version}-beta</tagNameFormat>
                  <autoVersionSubmodules>true</autoVersionSubmodules>
                  <suppressCommitBeforeBranch>true</suppressCommitBeforeBranch>
                  <remoteTagging>false</remoteTagging>
                </configuration>
              </plugin>
            </plugins>
          </build>
      </profile>

回答1:

The correct way to activate a profile is:

Remove the <activation> stanza and in your case do

mvn release:branch -P branch

This is all you should need given that you have hard coded ${project.version} in the configuration anyway.

There is no way to get ${project.version} without running the mvn command to begin with or parsing the pom.xml externally.

The documentation on this is pretty clear:

To create a branch execute this command:

mvn release:branch -DbranchName=my-branch

By default, the POM in the new branch keeps the same version as the local working copy, and the local POM is incremented to the next revision. If you want to update versions in the new branch and not in the working copy, run:

mvn release:branch -DbranchName=my-branch -DupdateBranchVersions=true -DupdateWorkingCopyVersions=false