Maven: generating a WAR for an entire project and a JAR of a specific package

I am developing a webapp using the controller-view-model pattern. The package model

is the main API of the application that can be used independently. Packages controller

and view

use a package model

to build a webapp.

Question: I would like to be able to create a package only JAR model

and a WAR of 3 packages. And I would like to keep three packages in one project. The Webapp WAR will include additional libraries over the standalone JAR. How could I achieve this with Maven? Will the solution use, for example, two separate pom.xml files? (since as far as I know you cannot select two options packaging

in the pom.xml)

(I saw this question , but I really want my three packages to be part of the same project and part of the same directory.)

Also, my project uses the classic webapp framework (simplified here):

my-app
-- pom.xml
-- src
----- main/
-------- java/
----------- model/
----------- view/
----------- controller/
-------- webapp/
---------- WEB-INF/
-------------classes/

      

Can I create JARs in classic directory target/

and webapp in classic directory src/main/webapp/

? (and, upon request, including various libraries)

Thank you for your help.

+3


source to share


2 answers


Have a look at Maven Build Plugin .

With it, you can have as many different assembly descriptors as possible, each of which is customizable with its own packaging scheme, filesets, and dependencies.


Here is an example plugin config for 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 an example assembly descriptor for the jar ( src/main/assembly/model.xml

) model

<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 it mvn clean package assembly:single

will create both the default my-app.war file from your pom.xml project and my-app-model.jar in the target package.

+7


source


I think you would prefer "war" as the standard packaging for the project, and an easier way is to use the maven-jar-plugin:



       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.4</version>
           <executions>
               <execution>
                   <phase>package</phase>
                   <goals>
                       <goal>jar</goal>
                   </goals>
                   <configuration>
                       <classifier>weibo4j</classifier>
                       <includes>
                           <include>model/**</include>
                       </includes>
                   </configuration>
               </execution>
           </executions>
       </plugin>

      

+3


source







All Articles