Configuring Perforce scm into maven project to get latest changelist

风格不统一 提交于 2019-12-04 06:51:56
randy-wandisco

I got some advice from one of the P4Maven developers that might help.

First, check your configuration. The "..." in the "" tag should be one of the tag names in the "" tag (i.e. "connection" or "developerConnection")

There two options to use Maven with Perforce SCM.

  1. Use the default (built-in) Maven Perforce SCM provider (p4 commandline based)

    • Note that you'll need the p4 commandline executable installed
    • You can set the username and password using environment variables or JVM args

[environment variables] P4CLIENT= P4USER= P4PASSWD=

or

[JVM args] -Dusername= -Dpassword=

[pom.xml] ...

  ...

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-scm-plugin</artifactId>
    <version>1.4</version>
  </plugin>

  ...

...

 <!-- SCM Settings -->
  <scm>
    <connection>scmerforce:localhost:1666://depot/someproject</connection>
    <developerConnection>scmerforce:localhost:1666://depot/someproject</developerConnection>
    <url>scmerforce://depot/simple</url>
  </scm>

...

  1. Use the P4Maven Perforce SCM provider (P4Java based)

[pom.xml]

...

  ...

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-scm-plugin</artifactId>
    <version>1.4</version>
    <dependencies>
      <!-- P4Maven -->
      <dependency>
        <groupId>com.perforce</groupId>
        <artifactId>p4maven</artifactId>
        <version>[2011,2012)</version>
      </dependency>
    </dependencies>
    <configuration>
      <connectionType>connection</connectionType>
      <username>someuser</username>
      <password>somepassword</password>
      <includes>**</includes>
    </configuration>
  </plugin>

  ...

...

scm4:localhost:1666://depot/someproject scm4:localhost:1666://depot/someproject scm4://depot/someproject

...

  • Note that for P4Maven we're overriding the default provider inside the "maven-scm-plugin" plugin.

  • Note that we're using "scmp4" (if using P4Maven) instead of "scmperforce" (built-in default) as the provider name, since "perforce" is taken by the existing default implementation.

I was struggling with exactly the same problem recently - I just wanted to get a Perforce revision number to use it in maven artifacts (for example as a part of a name). I checked buildnumber-maven-plugin, but it does not support Perforce at all. I also tried maven-release-plugin, but it seems to me as it does too much and I even didn't find out if it will do what I need.

Anyway I ended up with a solution which I don't like, but it works. I get this revision number directly with p4 executable via ant and antrun plugin (you must use the latest 1.7 version to export ant property to maven). You also need to have p4 executable available.

After this plugin configuration is used you have ${revision.number} available in maven.

<!-- Get Perforce latest change number -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
    </dependencies>
    <configuration>
        <exportAntProperties>true</exportAntProperties>
    </configuration>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="maven.plugin.classpath"/>
                    <!-- Login to p4 -->
                    <exec executable="p4" inputstring="${p4.password}">
                        <arg value="-p${p4.server}"/>
                        <arg value="-c${p4.client}"/>
                        <arg value="-u${p4.username}"/>
                        <arg value="login"/>
                    </exec>
                    <!-- Get reivision description text -->
                    <exec executable="p4" outputproperty="revision.description">
                        <arg value="-p${p4.server}"/>
                        <arg value="-c${p4.client}"/>
                        <arg value="-u${p4.username}"/>
                        <arg value="changes"/>
                        <arg value="-m1"/>
                        <arg value="//...#have"/>
                    </exec>
                    <!-- Logout from p4 -->
                    <exec executable="p4">
                        <arg value="-p${p4.server}"/>
                        <arg value="-c${p4.client}"/>
                        <arg value="-u${p4.username}"/>
                        <arg value="logout"/>
                    </exec>

                    <!-- Parse revision description to retrieve only revision number -->
                    <propertyregex property="revision.number"
                                   input="${revision.description}"
                                   regexp="Change ([0-9]*) on ([a-z,0-9]*)"
                                   select="\1"
                                   casesensitive="false"/>

                    <echo>Perforce latest revision number: ${revision.number}</echo>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

It's been a few years since the question was asked, but in the interval p4maven has been rewritten, and the updated documentation is startlingly hard to find.

Here's the new README.md. I followed it through and everything works. At this point, 1.0.6 is the latest version in maven central.

I discovered the source from a link on the Maven build number plugin page.

I landed here while searching for solution of a similar issue "NO Configuration could be determinded" which i faced with Code Collaborator client with P4. I ended up in uninstalling P4 client and reinstalling to get it working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!