Search code examples
javajarwarvs-web-application-projectmaven-war-plugin

Maven - maven-war-plugin to generate jar and install it to maven local repository


I am using maven-war-plugin in my pom.xml to generate a jar file in parallel with my war file in an java web project build. My maven build is creating war and jar files in the target directory. And only war file is installed to local repository. Is there a way to push the jar file created as well to local repository. Below is the snippet of my pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>             
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            </archive>
            <archiveClasses>true</archiveClasses>
        <packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
        <webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
         <webResources>
        <resource>
          <directory>${project.basedir}\src\main\webapp</directory>
          <includes>
            <include>**/*.*</include>
          </includes>
        </resource>
            </webResources>
    </configuration>
</plugin>

Thanks in advance!

pom.xml content:

<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>
    <groupId>com.xyz</groupId>
    <artifactId>x</artifactId>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>x</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>             
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        </archive>
                        <archiveClasses>true</archiveClasses>
                    <packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
                    <webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
                     <webResources>
                    <resource>
                      <directory>${project.basedir}\src\main\webapp</directory>
                      <includes>
                        <include>**/*.*</include>
                      </includes>
                    </resource>
                        </webResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
              <groupId>com.googlecode.addjars-maven-plugin</groupId>
              <artifactId>addjars-maven-plugin</artifactId>
              <version>1.0.2</version>
              <executions>
                <execution>
                    <goals>
                        <goal>add-jars</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/src/main/webapp/WEB-INF/lib</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
              </executions>
            </plugin>

        </plugins>
    </build>
</project>

Solution

  • This is not the right way to use maven. Maven is all about modularity and as a consequence there is a "one project - one artifact" rule (or recommendation). See also this blog if I can't convince you : How to Create Two JARs from One Project (…and why you shouldn't) . It is about multiple jars but the concept is the same.

    I think you should restructure your work into having one separate project for the jar, while the others use it as a dependency.