tags:

views:

733

answers:

5

I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.

How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?

+1  A: 

You should add the corresponding dependency of the artifact in the dependencies of the pom file.

Ex:

<dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
</dependency>
Warrior
Make an artifact dependent on itself? I don't understand how this would affect deployment. And in fact maven doesn't like the resulting cyclic reference.
Joshua Swink
+2  A: 

The "maven way" is to split out src/main/java into a separate module, and have the war file depend on that.

If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packaging element. I'm not sure if that's possible though.

Dominic Mitchell
+1  A: 

Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.

Brian Fox
A: 

One way to solve this is to have the module build a jar and then use the assembly plugin to build a war file with the jar in WEB-INF/lib of that war. I would strongly recommend against this. You'd be better off having a jar project and a war project with a parent project building both modules.

sal
A: 

This blog post and its comments have the answer.

These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>
                    ${project.build.directory}/${project.artifactId}-${project.version}.jar
                </file>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <executions>
        <execution>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <url>${project.distributionManagement.repository.url}</url>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>
yincrash