Charlie Wu
Charlie Wu

Reputation: 7757

how to add external jar to maven webapp project

I have a Spring roo project (basically a maven project). I want to add dropbox sdk to the project, problem is it's not in maven. I added the following files

<dependency>
    <groupId>com.dropbox</groupId>
    <artifactId>dropbox-sdk</artifactId>
    <version>1.3.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/dropbox-java-sdk-1.3.1.jar</systemPath>
</dependency>

It solved the compile error, but when i run the project, in Spring Tool Suite, the jar files are not added to war lib folder. How do I make maven add my external jar files to my the war lib folder?

I don't want to install the jar in maven since, I have to install it in all the machines that uses the project

Upvotes: 14

Views: 23786

Answers (8)

Charlie Wu
Charlie Wu

Reputation: 7757

I finally found a neat solution, which is a lot easier to implement. You add an in-project repository inside the java project and link to it in the pom.

You add an in-project repository in maven like this:

<repository>
    <id>in-project</id>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/libs</url>
</repository>

Then create a folder structure in the root folder of your project that looks something like this

/groupId/artifactId/version/artifactId-version.jar

and add the dependency as you would normally do.

This approach has the least amount of code and work required, and if that library ever gets add into a maven repository you can always remove your in-project repository.

Upvotes: 15

Haroldo_OK
Haroldo_OK

Reputation: 7230

The steps described in this site are pretty simple, and they work well enough: https://mythinkpond.com/2010/10/02/adding-custom-jars-under-web-inflib-in-a-maven-project/

  1. Create a “lib” folder under your project like this: “\src\main\webapp\WEB-INF\lib”
  2. Copy needed “jars” etc that you want included inside your WAR bundle folder.
  3. Invoke your maven build as you normally do. I use “mvn install”, which creates builds the war file.

Upvotes: 0

Aarav malhotra
Aarav malhotra

Reputation: 71

The best way to resolve this issue is to add these local jar files to WEB-INF/lib folder. You will find all these jars packaged in your final war file then.

Upvotes: 7

Stony
Stony

Reputation: 3624

There is a much easier solution, which is set webResource in the plugin. By the solution, you can add any files of your local disk to the war! A sample is as below,

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <warName>api</warName>
                <webResources>
                    <resource>
                        <directory>libs/</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

Upvotes: 8

wan
wan

Reputation: 1

  1. change the lib path to : src/main/webapp/WEB-INF/lib

  2. in pom.xml:

    <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/xxxx.jar</systemPath>

Upvotes: 0

user1801279
user1801279

Reputation: 1783

I know I am really late but I was wondering on why you would not put in the jar in the local repo in the .m2 file and add a reference to the pom from there ?

Upvotes: -1

Kkkev
Kkkev

Reputation: 4905

I don't recommend this approach, but you could add some POM configuration to install the 3rd-party dependency in a separate profile:

<profiles>
    <profile>
        <id>install-dependencies</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.3.1</version>
                    <executions>
                        <execution>
                            <id>install-dropbox-sdk</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>install-file</goal>
                            </goals>
                            <configuration>
                                <groupId>com.dropbox</groupId>
                                <artifactId>dropbox-sdk</artifactId>
                                <version>1.3.1</version>
                                <file>src/main/lib/dropbox-java-sdk-1.3.1.jar</file>
                                <packaging>jar</packaging>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <id>build</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <dependencies>
            <dependency>
                <groupId>com.dropbox</groupId>
                <artifactId>dropbox-sdk</artifactId>
                <version>1.3.1</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

There are two profiles here: install-dependencies and build. The first installs the dropbox-sdk dependency into your Maven repository and needs to be run once on every machine as follows:

mvn -Pinstall-dependencies validate

The second is enabled by default, and adds the Dropbox SDK as a dependency.

To be honest though, this isn't much better than running

mvn install:install-file -Dfile=src/main/lib/dropbox-java-sdk-1.3.1.jar -DgroupId=com.dropbox -DartifactId=dropbox-sdk -Dversion=1.3.1 -Dpackaging=jar

on every machine.

The other downside of this approach is that you'll have to add all dependencies of the dropbox-sdk to your build as well- whereas if it is done properly by adding the JAR and a POM to a repository server, then Maven will calculate the transitive dependencies properly.

Upvotes: 3

Kkkev
Kkkev

Reputation: 4905

I recommend creating a "third party" repository in a Maven repository server such as Nexus or Artifactory, and uploading the jar to there. Even though that means putting the jar into Maven, at least with a repository server it is available to anyone who will be building your application.

Upvotes: 2

Related Questions