Using Maven

Author: Tayeb Merabti
Small modifications: Paweł Guzewicz

  1. Maven versioning:
    In Maven, when you deploy a new version of your JAR file, this version will be downloaded only once to the local repository because Maven never re-downloads releases. For this reason, new releases should use a new version number. In other words, when you are using 1.0, once Maven has downloaded this artifact as a release, it never tries to get a new 1.0 from the remote repository.If the project is still under development, you should use the -SNAPSHOT version. In this case, Maven will check for updates, and download or re-download the latest version from the remote repository to the local since the snapshots modules are stored with a timestamp and a build number and can be easily tracked.
  2. Using SNAPSHOT versions:
    1. In pom.xml, under <version> tag change the name to version-SNAPSHOT (e.g. 0.0.1-SNAPSHOT).
    2. Before running mvn deploy, it is important to configure correctly the snapshot server in the project pom.xml and in the settings.xml file:
      1. In the pom.xml, before the closing </build> tag add the <distibutionManagement>…</distibutionManagement> element:
        [
        <build>



        <distributionManagement>
        <repository>
        <id>cedarcommons-private-release</id>
        <name>CEDAR-commons MVN release repo</name><url>http://maven.inria.fr/artifactory/cedarcommons-private-release</url></repository>
        <snapshotRepository>
        <id>cedarcommons-private-snapshot</id>
        <name>CEDAR-commons MVN snapshot repo</name>
        <url>http://maven.inria.fr/artifactory/cedarcommons-private-snapshot</url>
        </snapshotRepository>
        </distributionManagement>
        </build>
        ]
        where cedarcommons-private-release corresponds to the release repository and cedarcommons-private-snapshot to the snapshot repository.
      2. The settings.xml file under the ~/.m2 directory must contain all connection information to release and snapshot repositories.Under the <servers> element add this server, note the password for the snapshot repository is the same password for the release repository:
        <server>
        <id>cedarcommons-private-snapshot</id>
        <username>cedar-private-writer</username>
        <password>****</password>
        </server>Under the <repositories> element add:
        <repository>
        <id>cedarcommons-private-snapshot</id>
        <name>CEDAR commons MVN snapshot repo</name>
        <url>http://maven.inria.fr/artifactory/cedarcommons-private-snapshot</url>
        <layout>default</layout>
        <releases>
        <enabled>false</enabled>
        </releases>
        <snapshots>
        <enabled>true</enabled>
        </snapshots>
        </repository>
  3. Deployment
    Run mvn deploy to deploy the new snapshot version in the snapshot repository.
  4. Update:
    Run mvn clean install to update dependencies in the local repository with the latest snapshot version.
  5. Best practices (to be extended…):
    Before the deployment of any packages, be sure that the latest dependencies “release and/or snapshot” are in remote repositories.Example: Suppose that you have two maven projects: A and B. Project B is one of the dependencies of project A. Locally, you make some changes in project B and you need to deploy project A. Before the deployment of A in the remote repository you need to deploy the updated version of B first.
  6. Advanced tips (not finished) and how to use mvn release:prepare and mvn release:perform:
    It is possible to link a maven project with a GitLab repository. To do so, in the pom.xml file, you need to add:
    <scm>
    <url>https://gitlab.inria.fr/cedar/gitProjectName.git</url>
    <connection>scm:git:git@gitlab.inria.fr:cedar/gitProjectName.git</connection>
    <developerConnection>scm:git:git@gitlab.inria.fr:cedar/gitProjectName.git</developerConnection>
    </scm>

    Then, after the scm element, add the maven-release-plugin:
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
    <tagNameFormat>@{project.version}</tagNameFormat>
    <autoVersionSubmodules>true</autoVersionSubmodules>
    <releaseProfiles>releases</releaseProfiles>
    </configuration>
    </plugin>
    </plugins>
    Now, the project is configured with your GitLab repository and any change will be mentioned. For this, you can run mvn release:prepare.

  7. Useful commands:
    1. mvn dependency:analyze
      Finds explicit and implicit dependencies in the project and suggests to add or remove dependencies in pom.xml
    2. mvn versions:display-dependency-updates
      Shows the newest versions of your dependencies in pom.xml
    3. mvn versions:display-plugin-updates
      Shows the newest versions of plugins in pom.xml

Comments are closed.