Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Perforce scm into maven project to get latest changelist

I am migrating a project to Maven and as we were used to always reference our builds by the latest change number in our Perforce SCM repository, I would like to be able to extract this information

I am trying to configure Maven scm plugin via following resources:

  • http://maven.apache.org/scm/maven-scm-plugin/usage.html for generic usage
  • http://www.perforce.com/perforce/doc.current/manuals/p4maven/index.html

First I don't understand how to make it work, so if anybody has a fully working example, I will be happy to, on my side I have tried by adding in my pom:

<scm>
    <connection>
        scm:perforce:localhost:1666://depot/
        <my_project>
            /
            <version>
    </connection>
    <developerConnection>
        scm:perforce:localhost:1666:/depot/
        <my_project>
            /
            <version>
    </developerConnection>
    <url>http://somerepository.com/view.cvs</url>
</scm>
...
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.6</version>
        <dependencies>
            <!-- P4Maven -->
            <dependency>
                <groupId>com.perforce</groupId>
                <artifactId>p4maven</artifactId>
                <version>[2011,2012)</version>
            </dependency>
        </dependencies>
        <configuration>
            <connectionType>//depot/proto/kernel/kernel/04.00/maven2</connectionType>
            <username>my local username</username>
            <password>xxxxxx</password>
            <includes>**</includes>
        </configuration>
    </plugin>
</plugins>

this is leading me to:

[INFO] --- maven-scm-plugin:1.6:checkout (default-cli) @ kernel ---
mars 27, 2012 9:54:08 AM org.sonatype.guice.bean.reflect.Logs$JULSink warn
Avertissement: Error injecting: org.apache.maven.scm.provider.svn.svnexe.SvnExeScmProvider
java.lang.NoClassDefFoundError: org/apache/maven/scm/command/info/InfoScmResult

Surely forgot something, I will try to read again the instructions and see what I miss, but if anybody knows ...

Anyway, my question is rather: is it worth trying it ? I don't see in the available actions from scm plugin anything that will help me getting the last change information and integrate it into a reference build number. Shall I develop my own plugin for this ?

Thanks in advance.

like image 842
Gurvan Avatar asked Mar 27 '12 08:03

Gurvan


People also ask

What is SCM in Maven?

Maven SCM supports Maven plugins (for example maven-release-plugin) and other tools by providing them with a common API for source code management operations. You can look at the list of SCMs for more information on using Maven SCM with your favorite SCM tool.

Does Maven plugin allows for release of Maven projects?

This plugin is used to release a project with Maven, saving a lot of repetitive, manual work. Releasing a project is made in two steps: prepare and perform. Note: Maven 3 users are encouraged to use at least Maven-3.0.


2 Answers

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.

like image 65
randy-wandisco Avatar answered Oct 05 '22 09:10

randy-wandisco


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>
like image 28
arek Avatar answered Oct 05 '22 09:10

arek