How to include external jars in maven jar build pr

2020-02-11 17:46发布

I want to build a .jar file with dependencies in maven. Unfortunately I have to include some external .jars in my buildpath. When I now try to build this project with maven package I will get an error that those external .jars are not found.

How to adapt my pom file to add those jars? current:

    <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>

3条回答
The star\"
2楼-- · 2020-02-11 18:19

You need use below command to add external jar into .m2 folder

mvn install:install-file -Dfile=[JAR] -DgroupId=[some.group] -DartifactId=[Some Id] -Dversion=1.0.0 -Dpackaging=jar

This will add the given jar in to your .m2 folder. After that go to pom.xm and just add the dependency with given group id, artifact id and version.

查看更多
太酷不给撩
3楼-- · 2020-02-11 18:20

A simple solution for this is to add it into local maven repository

One way to do is via mvn install commands as suggested in previous post .

Another easy way is,

  1. In your eclipse ide right click on project select Maven option.
  2. Select Install or deploy an artifact to a maven repository option and click on next.
  3. Click on browse next to the Artifact file checkbox & select your jar file.
  4. Enter the GroupId and ArtifactId and version ensure generate pom & create checksum are checked & packaging is jar

Click on finish, Wallah!!! your job is done the jar is added in your local repository which you can define in setting.xml or m2 directory.

Now just add the simple maven dependency as per the GroupId,ArtifactId & jar version that you have entered as per the import and that's it your external jar will be packaged by maven.

查看更多
在下西门庆
4楼-- · 2020-02-11 18:36

You can include the external jars in your build path as dependency with <scope>system</scope> .

Check this link

查看更多
登录 后发表回答