Domanda

Edit

I was able to create a maven plugin, get the url info from the repository and fetch the info from svn:

@Mojo(name = "svn-info", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
@Execute(phase = LifecyclePhase.PROCESS_SOURCES)
public class SvnInfoMojo extends AbstractMojo {
    @Parameter(property="project.scm.url", defaultValue = "${project.scm.url}", required = true)
    private String url;
    private SVNInfo info;
    private Long svnLastRevision;
    private Date svnLastChangedDate;

    public void execute() throws MojoExecutionException, MojoFailureException {
        // the controller uses SVNKit to fetch the info
        SVNController controller = new SVNController(url);
        info = controller.getInfo();

        this.svnLastRevision = info.getCommittedRevision().getNumber();
        this.svnLastChangedDate = info.getCommittedDate();
    }
}

Now my question is:

How do I set the variables ${last-rev} and ${last-changed} in the project pom? I've tried the following:

@Parameter(property = "last-rev")
private Long svnLastRevision;

@Parameter(property = "last-changed")
private Date svnLastChangedDate;

but that didn't work, my test pom still shows ${last-rev}.


I am using Maven and Jenkins to build the project. I have been able to include properties from the pom file using the <properties> and <filtering>true</filtering>:

application.properties

application.revision=${project.version} #uses maven's version tag.
application.revision=Rev. ${last-rev} of ${last-changed}
application.build.type=${project.buildType} #depends on the maven profile

I am now trying to insert into the properties file the SVN Revision info in the following format:

application.revision=Rev. ${last-rev} of ${last-changed}

That would give me, for instance, Rev. 11981 of 2014-02-03 11:01:20 -0200 (Mon, 03 Fev 2014).

This is what I have tried so far:

  1. Calling a shell script during a Jenkins pre-build job.

    Although this works (using svn info and sed commands), it's platform dependent, which is not ideal.

  2. Using Build Number Maven Plugin.

    My problem is that it gives me the SVN Revision number, not the Last Changed Rev.

    Plus, if I try to format the revision to the format I want (using the <format> configuration option), it changes the ${buildNumber} to an internal one and uses the build date (ex: Rev. 1 of 2014-02-04 15:03:57 -0800 (Tue, 04 Fev 2014)).

  3. Creating my own Maven Plugin using SVNKit.

    In order to do that I'd need to:

    • Get the scm connection from the project pom;
    • create a SVNUrl based on the scm url received;
    • Use SVNWCClient.doInfo(url, SvnRevision.HEAD);
    • Create variables usable by the project pom file (such as ${last-rev} and ${last-changed}

In the latter case, I am new to the maven plugin API. I don't really know how to get the Scm component or create variables usable by the project pom. I've tried using @Component private Scm scm; but that did not work.

Any ideas on how I could add Last Changed Rev. and Last Changed Date into my properties file?

È stato utile?

Soluzione 2

I was able to solve both problems (getting the Last Changed Rev and creating my Maven Plugin)! I won't be needing the maven plugin, but I may come in handy later on (i.e if the Jenkins Subversion Plugin later changes the ${SVN_REVISION} variable to the repository revision).

Getting the Last Changed Rev.

For the first problem, I misread the Jenkins Subversion Plugin documentation. The ${SVN_REVISION} environment variable does return the Last Changed Rev number, not the repository revision one. So, I was able to do this in my pom:

pom.xml

<properties>
    <!-- this default revision number will be changed by Jenkins -->
    <svn.revision>REV</svn.revision>
    <!-- other properties -->
</properties>

Then, in the Jenkins build task, I call maven using package -Dsvn.revision=${SVN_REVISION}, which will then change the svn.revision parameter in Maven.

Maven Plugin

In order to set the parameters in my Maven plugin, I've use the class MavenProject, which gives me the project pom properties:

@Mojo(name = "svn-info", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
@Execute(phase = LifecyclePhase.PROCESS_SOURCES)
public class SvnInfoMojo extends AbstractMojo {
    @Component
    private MavenProject project;
    @Parameter(property="project.scm.url", defaultValue = "${project.scm.url}", required = true)
    private String url;
    private String svnLastRevision;
    private String svnLastChangedDate;

    public void execute() throws MojoExecutionException, MojoFailureException {
        SVNController controller = new SVNController(url);
        info = controller.getInfo();

        this.svnLastRevision = Long.toString(info.getCommittedRevision().getNumber());
        // Sets the property ${last-rev} in the project pom
        project.getProperties().setProperty("last-rev", this.svnLastRevision);

        // uses DateFormat to parse the date
        this.svnLastChangedDate = parseDate(info.getCommittedDate());
        // Sets the property ${last-changed} in the project pom
        project.getProperties().setProperty("last-changed", this.svnLastChangedDate);
    }
}

Altri suggerimenti

  1. Calling a shell script during a Jenkins pre-build job.

    Although this works (using svn info and sed commands), it's platform dependent, which is not ideal.

    Furthermore if you use svn info you will get SVN Revision number and not the last changed Rev. Apart from that there are better tools to get such information svnversion.

  2. Using Build Number Maven Plugin.

    My problem is that it gives me the SVN Revision number, not the Last Changed Rev.

    This gives me the impression that you didn't understand the differences between SVN revision number and the last changed rev. If you like to have the last changed rev you have to say about which file? Usually you like to have the last changed revision of your trunk which is exactly what the build number maven plugin will provide (as well svn info or svnversion).

  3. Creating my own Maven Plugin using SVNKit.

    You will go the same way as the build-number-maven-plugin is working.

You can use the following configuration for buildnumber-maven-plugin to get the svn revision you should use:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
      <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
      <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>create</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

By using the above configuration you can use the ${buildNumber} which contains the SVN revision number.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top