Nageswaran
Nageswaran

Reputation: 7651

Maven pom.xml with more than one jar with version

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <url>http://maven.apache.org</url>

    <name>SomeProject</name>

    <groupId>com.test.te</groupId>
    <artifactId>testjar</artifactId>
    <version>1.1.6</version>
    <packaging>jar</packaging>

    <dependencies>
    ....
    ....
    </dependencies>
    <build>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.conf</include>
                    <include>**/*.java</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <executions>
                    <execution>
                        <id>drive</id>
                        <goals><goal>jar</goal></goals>
                            <phase>install</phase>
                            <configuration>
                                <finalName>driver</finalName>
                                <includes>
                                    <include>com/test/te/p1/*.java<include>
                                    <include>com/test/te/p1/*.class</include>
                                </includes>
                            </configuration>
                        </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Here is how I am executing this

mvn clean install

Here is the build output's last few lines.

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/mycp/workspace/SomeProject/target/testjar-1.1.6.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing /home/mycp/workspace/SomeProject/target/testjar-1.1.6.jar to /home/mycp/.m2/repository/com/test/te/testjar/1.1.6/testjar-1.1.6.jar
[INFO] [jar:jar {execution: driver}]
[INFO] Building jar: /home/mycp/workspace/TestProject/target/driver.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11 seconds
[INFO] Finished at: Wed Oct 28 17:27:10 IST 2015
[INFO] Final Memory: 129M/888M
[INFO] ------------------------------------------------------------------------

How to do this?

My requirement is create two jars testjar-${project.version}.jar driver-${project.version}.jar and install those two in two different artifact

/home/mycp/.m2/repossitory/com/test/te/testjar/${project-version}/testjar-${project.version}.jar

/home/mycp/.m2/repossitory/com/test/te/driver/${project-version}/driver-${project.version}.jar

Upvotes: 0

Views: 1045

Answers (1)

Tunaki
Tunaki

Reputation: 137064

Why the 2nd jar (driver.jar) is not versioned (driver-1.1.6.jar) ?

That is because you explicitely told the maven-jar-plugin that its name should be driver with <finalName>driver</finalName>. If you want to the version to be appended, you will need to have the following configuration: <finalName>driver-${project.version}</finalName>.

Why the 2nd jar not installed in local repo?

That is because you have specified a phase of <phase>install</phase>, so it will executed after the default-install execution, which is the execution that installs artefact to the local repository. Instead, you should bind that execution to the package phase.


This is the configuration you should have for the maven-jar-plugin. A classifier is needed to create another jar.

<execution>
  <id>drive</id>
  <goals>
    <goal>jar</goal>
  </goals>
  <phase>package</phase>
  <configuration>
    <finalName>driver-${project.version}</finalName>
    <classifier>classifier</classifier>
    <includes>
      <include>com/test/te/p1/*.java</include>
      <include>com/test/te/p1/*.class</include>
    </includes>
  </configuration>
</execution>

As a side note, I noticed that you also configured a <resources> element in your POM including Java and conf files. You should not have that. More specifically: all the resources should be placed under src/main/resources and all Java files under src/main/java. Then there is no need for that configuration.


Following from the comments, if you can't add a classifier, then you have to drop maven-jar-plugin and use maven-assembly-plugin instead. This is a simple assembly descriptor that will get you started. Place this file under src/main/assembly/assembly.xml.

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>driver</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <unpackOptions>
                <includes>
                    <include>com/test/te/p1/*.class</include>
                </includes>
            </unpackOptions>
            <unpack>true</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

with the following configuration in the POM:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor> <!-- point to the location of the assembly.xml file in your project structure -->
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
        <finalName>driver-${project.version}</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Upvotes: 3

Related Questions