简体   繁体   中英

Maven : How to package external jars(folder) in Maven project

I am having a group of external jars(in hundreds) which I have added in the build path of my project to make it work. But while packaging it is failing as these jar's are not available to maven.

I have gone though many articles and all the solutions(like adding the jar at system path) are for a single jar only.

<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>

Is there any way we can add the group of jars(folder) to the packaging on the project? or any other solution by which my project can build successfully?


can we create a single jar containing all my jars inside and then use the system scope of maven

I have tried creating jar by jar -cvf my_jar.jar * and placed this jar in the system scope. But it does not worked for me.

The bad news: For a proper Maven project, you need to add each and every artifact you use as <dependency> in your POM.

The good news: I very much doubt that these 100 jars are all - directly used in your source code - not available in a public Maven repository like MavenCentral

So the better strategy would be to figure out what you really need and find that in MavenCentral. Then Maven finds all the transitive dependencies for you. So if you really need 10 of the jars and all other jars are just dependencies of your dependencies, just add these 10 ones (from MavenCentral) and you are done.

My solution: Maven pluggin addjar let us add all jar at a place(projectdirectory/lib in this case).

this enables you to add these jar's in the final package(jar in my case) when you maven build, but to run locally you have to add those jar files directly in the classpath.

            <plugin>
                      <groupId>com.googlecode.addjars-maven-plugin</groupId>
                      <artifactId>addjars-maven-plugin</artifactId>
                      <version>1.0.5</version>
                      <executions>
                             <execution>
                                   <goals>
                                          <goal>add-jars</goal>
                                   </goals>
                                   <configuration>
                                          <resources>
                                                <resource>
                                                       <directory>${basedir}/lib</directory>
                                                </resource>
                                          </resources>
                                   </configuration>
                             </execution>
                      </executions>
            </plugin>

Now create a shade jar using mvn clean install shade:shade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM