07/2/12

Run Maven with Ant

Run Maven with Ant

If you have the option to remove Ant from your projects its probably easier to just use maven for most of the build process, but sometimes it requires too much work and time, so this is a quick solution to have your Ant script run maven.

<exec executable="cmd">
<arg value="/c" />
<arg value="mvn package -e -f path/to/pom.xml " />
</exec>

Maven command to skip tests

-Dmaven.test.skip=true

07/2/12

Deploy to Local Maven Repository

Deploy Snapshot or release jar to Local Maven Repository

In Settings.xml

<settings>
<servers>
<server>
<id>releases</id>
<username>username</username>
<password>password</password>
</server>
</servers>
</settings>

In pom.xml


<distributionManagement>
<repository>
<id>releases</id>
<url>http://server/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://server/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
07/1/12

Copy Maven Dependencies to target folder

The following maven xml will retrieve all the maven dependencies and copy them to the target/lib folder.

<plugin><!-- Copy maven dependencies to the lib folder -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <encoding>UTF-8</encoding>
        <outputDirectory>target/lib</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
      </configuration>
    </execution>
  </executions>
</plugin>
07/1/12

Add external jars to runnable jar’s manifest file using maven

Add external jars to runnable jar MANIFEST.MF file using maven

Also package project as a runnable jar with the specified main class

<plugin><!-- Set Main class and location of jars to add to the manifest classpath -->
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifestEntries><!-- Add manifest Classpath entries that aren't managed by maven -->
              <Class-Path>lib/pojoxml-1.0.jar lib/lwjgl.jar lib/jorbis-0.0.15.jar lib/jogg-0.0.7.jar lib/natives-win32.jar lib/slick.jar</Class-Path>
            </manifestEntries>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib</classpathPrefix>
              <mainClass>com.porto.game.game2d.GameMain</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
07/1/12

Packaging External Jars with Maven

<plugin><!-- Copy Dependencies - NonMaven Jars -->
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.5</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <encoding>UTF-8</encoding>
        <outputDirectory>target/lib</outputDirectory>
        <resources>
          <resource>
            <directory>lib</directory>
            <filtering>false</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

If filtering is set to true, Maven will parse the file using the encoding and rewrite it, I have found that his can cause all sorts of issues including class not found errors.