13

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

Charlie Wu
  • 7,657
  • 5
  • 33
  • 40

8 Answers8

15

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Charlie Wu
  • 7,657
  • 5
  • 33
  • 40
  • 1
    very nice! to get a groupId like "com.some.groupId.with.dots" working use directory structure `com/some/groupId/with/dots/artifactId/version/...` – Shlomo Georg Konwisser Jan 08 '16 at 20:09
  • Good approach. Works well on Linux. But Windows sucks. **Failure to find com.project.maven.plugin:libname:jar:7.20.1.1 in file://D:\workspace\myApplication/lib** - @Nathan Hughes – smilyface Jun 19 '18 at 11:01
  • Hi, there's something wrong with the link, can you replace it a working one? – Amir Shabani May 29 '19 at 13:46
7

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>
Stony
  • 3,541
  • 3
  • 17
  • 23
7

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.

  • 1
    This is as simple as it gets , and it works. Why has no-one else suggested this? – DS. Oct 11 '18 at 10:44
3

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.

Kkkev
  • 4,716
  • 5
  • 27
  • 43
2

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.

Kkkev
  • 4,716
  • 5
  • 27
  • 43
  • i don't want to run any extra server for the repo, is there any public repo i can utilize? – Charlie Wu May 20 '12 at 04:52
  • Even if you sort out the dropbox dependency, you'll hit many other problems without using in-house repository. If you don't want to manage additional server, you can always use the SaaS version - http://www.jfrog.com/art-online.php – JBaruch May 20 '12 at 08:00
  • See http://stackoverflow.com/questions/10533131/dropbox-sdk-repository-maven - the dropbox SDK has no public Maven repository at the moment. – Kkkev May 20 '12 at 18:30
0
  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>

wan
  • 1
  • 1
0

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.
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
-1

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 ?

user1801279
  • 1,743
  • 5
  • 24
  • 40
  • the problem require solving is to make the project portable, someone else check out this project will not have jar in their local repo – Charlie Wu Sep 18 '13 at 23:27