Maven: generating a WAR of the whole project, and a JAR of a specific package
java, maven
Solution
Take a look at Maven Assembly Plugin.
With it you can have as many different Assembly Descriptors as you need, each one customized with its own packaging scheme, filesets and dependencies.
Here is a sample plugin configuration for your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/model.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
And a sample assembly descriptor for the model jar (`src/main/assembly/model.xml`)
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>model</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}/model</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Running `mvn clean package assembly:single` will generate both the standard my-app.war file from your project pom.xml and a my-app-model.jar in the target package.
Problem
I develop a webapp using a model-view-controller pattern. The `model` package represents the core API of the application, that could be used independently. The `controller` and `view` packages use the `model` package to produce a webapp. Question: I'd like to be able to build a JAR of only the `model` package, and a WAR of the three packages. And I'd like to keep the three packages in a same project. The webapp WAR would include supplementary libraries as compared to the standalone JAR. How could I achieve this with Maven? Would there be a solution using for instance two separate pom.xml files? (because as far as I know, you cannot choose two `packaging` options in the pom.xml) (I've seen this question, but I really want my three packages to be part of a same project, and be part of the same directory.) Also, my project uses a classical webapp structure (simplified here): ``` my-app -- pom.xml -- src ----- main/ -------- java/ ----------- model/ ----------- view/ ----------- controller/ -------- webapp/ ---------- WEB-INF/ -------------classes/ ``` Could I manage to generate the JAR in the classical `target/` directory, and the webapp in the classical `src/main/webapp/` directory? (and, as asked, including different libraries) Thanks for your help.