46

I've searched on the Internet for quite some time, and I'm unable to figure out how to configure the maven-war plugin or something alike so that the system dependencies are included in the built-war (WEB-INF/lib folder).

I use the Maven dependency plugin in case of a JAR build as:

<plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> 

But I'm unable to understand what is to be done in case of a WAR build. I've tried using the maven-war plugin, but it's not including system dependencies in the build.

I'm having dependencies of type:

<dependency> <groupId>LoginRadius</groupId> <artifactId>LoginRadius</artifactId> <scope>system</scope> <version>1.0</version> <systemPath>${basedir}\lib\LoginRadius-1.0.jar</systemPath> </dependency> 

in my POM file, and these dependencies are not included in WEB-INF/lib when the WAR file is build.

5
  • 1
    What exactly do you mean by system dependencies? Commented Sep 28, 2013 at 10:02
  • I have included 3 jars in system scope in the project and I want them to be included in the WEB-INF/lib folder along with other dependencies in the final built war Commented Sep 28, 2013 at 10:20
  • possible duplicate of Maven 2 assembly with dependencies: jar under scope "system" not included Commented Sep 28, 2013 at 10:29
  • 1
    See also stackoverflow.com/questions/33799773/… Commented Jul 25, 2017 at 15:21
  • How to know the groupID? Not all jar file is straighforward as the name of jar itself. E.g. annotations-2.14.7.jar has the groupID of "sofware.amazon.awssdk' Commented Feb 28, 2023 at 9:18

7 Answers 7

28

Let me try to summarise the options I tried:

<packagingIncludes>${java.home}/lib/jfxrt.jar</packagingIncludes> 

This doesn't work! Also, only having the JAR file name, excludes everything else, so if you are willing to try then try

<packagingIncludes>${java.home}/lib/jfxrt.jar,**/*</packagingIncludes> 

Jatin's answer seemed a bit complex, and I tried going through the POM file again and again to figure out where exactly were the system JAR files mentioned to be included in WEB-INF POM.

Anyway, I ended up using this solution, which wasn't working at first, but after some time and some corrections worked:

I installed the JAR file in my local repository using the below command:

mvn install:install-file -Dfile="C:\Users\hp\Documents\NetBeansProjects\TwitterAndLoginRadiusMaven\lib\LoginRadius-1.0.jar" -DgroupId=LoginRadius -DartifactId=LoginRadius -Dversion=1.0 -Dpackaging=jar` 

After running the above command, I changed the dependency in POM to:

<dependency> <groupId>LoginRadius</groupId> <artifactId>LoginRadius</artifactId> <!--<scope>system</scope>--> <version>1.0</version> <!--<systemPath>${basedir}\lib\LoginRadius-1.0.jar</systemPath>--> </dependency> 

NOTE - See I've commented the system scope and systemPath.

Building the WAR file now, includes this LoginRadius-1.0.jar file in WEB-INF/lib.

Sign up to request clarification or add additional context in comments.

5 Comments

So, the Maven "fundamentalists" don't care that you now cannot have a 2 step build consisting of (ONLY) the steps: (1) Fetch from source control; (2) type a single build command (whether it's mvn, ant, make or build.{sh|bat}). This "solution", which I'm not blaming you for, it just seems to be part of Maven, now has a dependency that you either run this magic "install" incantation to put stuff in ~/.m2/ cache directory, OR, possess a work-group "Nexus" type cache in front of Maven Central which you also have permission to "deploy" things into.
Maven's dependency management model works great when you are pulling in Apache and Spring libraries, but not so much when you have libraries (jars) from vendors who do not want their stuff in Maven Central :-(
I agree. The need to put everything into repositories is a real inadequacy in Maven, since there actually could be reasons why someone may not want to do so (like having a single small jar that isn't worth creating a whole repo for). This really needs to be corrected. Meanwhile, I am switching to gradle...
This would be fine so long as the third party library allows the jar to be renamed. Some libraries from SAP do not allow the renaming that maven uses. So I have to continue using a dependency with a system scope.
Just use a a blank <includeScope></includeScope>. See maven.apache.org/plugins-archives/maven-dependency-plugin-3.1.2/…
23

If by chance you can't install the third-party library to your local repository, due to some silly naming/packaging checks by the third party, you can still add your system scoped dependencies to your final package at build time (at least if you are building a web application) using the maven-war-plugin where you would need to create a configuration like this:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> <configuration> <failOnMissingWebXml>true</failOnMissingWebXml> <webResources> <resource> <directory>path/to/lib/in/project</directory> <targetPath>WEB-INF/lib</targetPath> </resource> </webResources> </configuration> </plugin> 

I am not sure, but I believe that the library must be somewhere local to the project's base directory. I tend to create a directory under src/main/ called lib to hold these sorts of third-party libraries. During the build process, they are placed in the correct directory and added to the WAR file.

2 Comments

if i do this. it is necessary to include the libraries as dependencies also? or do i have to remove the system dependencies?
@elbraulio I'm guessing you are asking if you need to specifically add includes for the normal dependencies. The standard dependencies (compile in scope) are handled normally, this should only target the ones that are system in scope.
8

You can configure the WAR plugin to have all or some JAR files included or excluded as per your need as mentioned below. It is simple and works.

<plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <webResources> <resource> <directory>${project.basedir}\lib</directory> <targetPath>WEB-INF/lib</targetPath> <filtering>false</filtering> <includes> <include>**/*.jar</include> </includes> <excludes> <include>**/javax.servlet-api-3.1.0.jar</include> </excludes> </resource> </webResources> </configuration> </plugin> 

Comments

4

If you meant as JAR file dependencies, then below is a sample pom.xml file which takes all needed files and generates a WAR file:

<build> <defaultGoal>install</defaultGoal> <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> <outputDirectory>${project.basedir}/target/classes</outputDirectory> <testOutputDirectory>${project.basedir}/target/test-classes</testOutputDirectory> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> </testResource> </testResources> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> <debug>true</debug> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <includeEmptyDirs>true</includeEmptyDirs> <webResources> <resource> <directory>ui</directory> <targetPath></targetPath> <includes> <include>**</include> </includes> </resource> <resource> <directory>lib</directory> <targetPath>WEB-INF</targetPath> <includes> <include>**/*.xml</include> <include>**/log4j.properties</include> </includes> </resource> // Edited below <resource> <directory>lib</directory> <targetPath>WEB_INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> <webXml>${project.basedir}/WEB-INF/web.xml</webXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> 

6 Comments

Where does it say that the system-dependencies (a few jars) will go into the WEB-INF/lib folder in the build.
@coding_idiot the maven-war-plugin automatically does that by putting all your dependencies in the WEB-INF/lib folder
where do you specify the system jars ? I was unable to see any jar or jar-location specified anywhere.
<resource><directory>lib</directory> <targetPath>WEB_INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> This code doesn't include system dependencies that I added, in the resultant war. I've also modified my question to specify how I've included system dependencies in my POM.
This worked for me. Beware : the example code includes a "maven-compiler-plugin" for Java version 1.5, I do not needed that. Also, it is not "WEB_INF" but "WEB-INF"!!!
|
3

Based on your initial POM file, I would suggest to send them directly to the WEB-INF/lib directory:

<goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/lib</outputDirectory> </configuration> 

1 Comment

jdevora's approach did not work for me. I got the .jar packaged into WEB-INF/lib as jdevora wrote, when building the WAR with mvn command line tool. But when running Wildfly server directly from eclipse, the eclipse deployment didn't use the pom copy task nor the .war compiled before. It deployed based on eclipse's maven dependencies, and so it skipped these JARs.
0

Use:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file \ -Dfile=~/Desktop/medici-1.0.1.jar \ -DgroupId=com.hp \ -DartifactId=medici \ -Dversion=1.0.1 \ -Dpackaging=jar \ -DlocalRepositoryPath=lib 

If all went well, you can find your artifact published inside of lib.

find lib 

Output:

lib lib/com lib/com/hp lib/com/hp/medici lib/com/hp/medici/1.0.1 lib/com/hp/medici/1.0.1/medici-1.0.1.jar lib/com/hp/medici/1.0.1/medici-1.0.1.pom lib/com/hp/medici/maven-metadata-local.xml Note the structure here mimics what you’d find in ~/.m2. 

Now, in your pom.xml file, declare it a Maven repository in your project:

<repositories> <repository> <id>local-repo</id> <url>file://${basedir}/lib</url> </repository> </repositories> 

And lastly, in your pom.xml file, declare a dependency on the local .jar file like you would for any dependency.

<dependencies> <dependency> <groupId>com.hp.medici</groupId> <artifactId>medici</artifactId> <version>1.0.1</version> </dependency> </dependencies> 

Comments

0

It may be problem of the scope where the dependencies are defined:

  • compile: Dependencies in "compile" scope will automatically be copied to the target's WEB-INF/lib as part of the Maven build.
  • provided: Dependencies in "provided" scope will not be copied (you would use this for server-supplied JAR files such as the JSP-api.jar file).

To generate a WAR file in Maven, two goals have to be run: "compile" and "war:war". Running the WAR goal alone won't compile the Java source. If you prefer, run one of the master goals, such as "package".

If using Eclipse:

Select the POM file in the project explorer and select the "Run" context menu option. This will build and execute a Maven run profile (or use an existing one). Maven run profiles behave just like regular run/debug profiles, except that their profile edit dialogs support Maven-specific features.

Source: How to include Maven dependencies in a WAR file (WEB-INF/lib) deployed using the m2eclipse plugin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.