如何把所有必需的JAR文件的库文件夹使用Maven最终JAR文件里面?(How do I put a

2019-06-17 14:54发布

I am using Maven in my standalone application, and I want to package all the dependencies in my JAR file inside a library folder, as mentioned in one of the answers here:

How can I create an executable JAR with dependencies using Maven?

I want my final JAR file to have a library folder that contains the dependencies as JAR files, not like what the maven-shade-plugin that puts the dependencies in the form of folders like the Maven hierarchy in the .m2 folder.

Well, actually the current configuration does what I want, but I am having a problem with loading the JAR files when running the application. I can't load the classes.

Here's my configuration:

<plugins>

    <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-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.myapp.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <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>install</id>
                <phase>install</phase>
                <goals>
                    <goal>sources</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

</plugins>

The project runs fine from Eclipse, and the JAR files are put in the library folder inside my final JAR file as I want, but when running the final JAR file from the target folder I always get ClassNotFoundException:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.myapp.MainClass. Program will exit.

How can I fix this exception?

Answer 1:

以下是我的解决方案。 测试它是否适合你:

<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-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <!-- <classpathPrefix>lib</classpathPrefix> -->
                <!-- <mainClass>test.org.Cliente</mainClass> -->
            </manifest>
            <manifestEntries>
                <Class-Path>lib/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

第一插件将所有依赖关系中的目标/类/ lib文件夹,而第二个包含在最终的JAR文件中的库文件夹,并配置Manifest.mf文件。

但随后你将需要添加自定义类加载代码加载的JAR文件。

或者,为了避免自定义类加载,你可以用“$ {} project.build.directory / lib下,但在这种情况下,你没有最终的JAR文件,该文件违背了目的的内部依赖关系。

它已经两年有人问。 嵌套JAR文件的问题仍然存在。 我希望它可以帮助别人。



Answer 2:

更新:

<build> 
  <plugins> 
    <plugin> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
      <execution> 
        <phase>install</phase> 
          <goals> 
            <goal>copy-dependencies</goal> 
          </goals> 
          <configuration> 
             <outputDirectory>${project.build.directory}/lib</outputDirectory> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 
  </plugins> 
</build> 


Answer 3:

最简单和最有效的方式是使用超级插件是这样的:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>uber-${artifactId}-${version}</finalName>
            </configuration>
        </plugin>

你将有非归在同一个JAR文件。



Answer 4:

该可执行打包机Maven插件可以用来正是出于这个目的:创建包含特定文件夹中的所有依赖关系的JAR文件独立的Java应用程序。

只需以下添加到您pom.xml里面的<build><plugins>部分(一定要取代的价值mainClass相应的):

<plugin>
    <groupId>de.ntcomputer</groupId>
    <artifactId>executable-packer-maven-plugin</artifactId>
    <version>1.0.1</version>
    <configuration>
        <mainClass>com.example.MyMainClass</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>pack-executable-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

内置JAR文件位于target/<YourProjectAndVersion>-pkg.jar运行后mvn package 。 所有的编译时和运行时的依赖也将被包含在lib/ JAR文件夹内。

免责声明:我是插件的作者。



Answer 5:

下面这个链接:

如何:Eclipse中的Maven安装使用依赖关系生成JAR

我发现这不是可行的解决方案,因为类加载器不会从罐中装入罐子,所以我想我会解开罐内的依赖关系。



Answer 6:

Here's我怎么做:

    <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.project.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

然后我就跑:

mvn assembly:assembly


Answer 7:

我发现这个问题的答案:

http://padcom13.blogspot.co.uk/2011/10/creating-standalone-applications-with.html

你不仅可以得到一个lib文件夹中的相关LIB文件,你还可以得到斌主任既具有UNIX和DOS可执行文件。

可执行最终调用与列出了所有的依赖库的太一-cp参数的java。

整个地段坐落在目标文件夹内的文件夹appasembly。 史诗。

=============是的,我知道这是一个古老的线程,但它仍然高即将于搜索结果,所以我想它可能会帮助我这样的人。



Answer 8:

这显然是一个classpath的问题。 考虑到,当你在IDE外部运行程序类路径必须改了一下。 这是因为IDE加载相对于项目的根文件夹中的其他JAR,而在最后的JAR的情况下,这通常是不正确的。

我喜欢在这种情况下做的是手工打造的JAR。 我花了最多5分钟,它总是解决问题。 我不建议你这么做。 找到一种方法,使用Maven,这是它的目的。



文章来源: How do I put all required JAR files in a library folder inside the final JAR file with Maven?