We will show here some tips to enrich your pom.xml configuration.

Setting the release version

One of the first thing we have coded in pom.xml is the release version. This has been injected into a property

<properties>
      <version.infinispan>5.1.5.FINAL</version.infinispan>
</properties>

And then used, when declaring the dependency

<version>${version.infinispan}</version>

In general, it is not always a best practice to design software which depends on a non-specific version of an artifact. If you are developing software, you might want to use the latest (LATEST) or latest stable release (RELEASE) as a convenience so that you don't have to update version numbers when a new release of a third-party library is released.
Let's see some alternatives you can choose to specify the release:

Declare an exact version (will always resolve to 5.1.5.FINAL):

<version>[5.1.5.FINAL]</version>

Declare an explicit version (will always resolve to 5.1.5.FINAL unless a collision occurs, when Maven will select a matching version):

<version>5.1.5.FINAL</version>

Declare a version range:(will currently resolve to the version defined as RELEASE, in your case 5.1.5.FINAL):

<version>[5.1.5.FINAL,5.2.0.Beta2)</version>

Declare an open-ended version range (will resolve to 5.2.0.Beta2, if that is the latest version):

<version>[5.1.5.FINAL,)</version>

Declare the version as LATEST (will resolve to 5.2.0.Beta2):

<version>LATEST</version>

Delcare the version as RELEASE (will resolve to the latest stable RELEASE 5.1.5.FINAL)

<version>RELEASE</version>

Enforcing the JDK and Maven release

If you want to use a specific JDK and Maven release you can use a Maven plugin. Maven is - at its heart - a plugin execution framework; all work is done by plugins. You can find a list of available plugins here. In our case we will use the maven-enforcer-plugin which is a Environmental constraint checking (Maven Version, JDK etc) and user custom Rule Execution.

   <build>
      <plugins>

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0-beta-1</version>
            <executions>
               <execution>
                  <id>enforce-java</id>
                  <goals>
                     <goal>enforce</goal>
                  </goals>
                  <configuration>
                     <rules>
                        <requireJavaVersion>
                           <version>[1.6,)</version>
                        </requireJavaVersion>
                        <requireMavenVersion>
                           <version>[2.1.0,)</version>
                        </requireMavenVersion>
                     </rules>
                  </configuration>
               </execution>
            </executions>
         </plugin>

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
               <source>1.6</source>
               <target>1.6</target>
               <encoding>UTF-8</encoding>
            </configuration>
         </plugin>
      </plugins>
   </build>

 You can add this plugin section, just after the dependency section of your pom.xml

Adding profiles to your pom.xml

By using profiles you can adapt the execution of your project to specific environments. For example you can create a "run" profile which will be used to trigger a Java main class, in our example the com.sample.Application class.

<profiles>
      <profile>

         <id>run</id>
         <activation>
            <activeByDefault>false</activeByDefault>
         </activation>
         <build>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <version>1.1</version>
                  <executions>
                     <execution>
                        <phase>process-classes</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                     </execution>
                  </executions>
                  <configuration>
                     <mainClass>com.sample.Application</mainClass>
                  </configuration>
               </plugin>
            </plugins>
         </build>
      </profile>
</profiles>

This can be useful if the user does not know which class to be launched at startup, so instead of running the project with:

mvn compile exec:java -Dexec.mainClass=com.sample.Application

Just use:

mvn install -Prun

Notice the -P option which is used to reference the "run" profile.

0
0
0
s2smodern