how to add external jar to maven webapp project

后端 未结 7 786
清歌不尽
清歌不尽 2020-12-31 12:26

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



        
相关标签:
7条回答
  • 2020-12-31 13:02
    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>

    0 讨论(0)
  • 2020-12-31 13:05

    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.

    0 讨论(0)
  • 2020-12-31 13:12

    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.

    http://bit.ly/OGVHSN

    0 讨论(0)
  • 2020-12-31 13:12

    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.

    0 讨论(0)
  • 2020-12-31 13:13

    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>
    
    0 讨论(0)
  • 2020-12-31 13:22

    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 ?

    0 讨论(0)
提交回复
热议问题