Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用maven将本地jar作为依赖项添加到胖jar中?_Java_Maven_Jar_Fatjar - Fatal编程技术网

Java 如何使用maven将本地jar作为依赖项添加到胖jar中?

Java 如何使用maven将本地jar作为依赖项添加到胖jar中?,java,maven,jar,fatjar,Java,Maven,Jar,Fatjar,我正试图建造一个肥罐子,以便在其他地方使用。 我使用了maven汇编插件: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> <configuration>

我正试图建造一个肥罐子,以便在其他地方使用。
我使用了
maven汇编插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>

    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

org.apache.maven.plugins


更容易使用shade插件:

父项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.greg</groupId>
  <artifactId>fat-jar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>library-jar</module>
    <module>final-jar</module>
  </modules>

</project>

4.0.0
com.greg
肥罐
1.0-快照
聚甲醛
藏书罐
最后一罐
图书馆项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>fat-jar</artifactId>
    <groupId>com.greg</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>library-jar</artifactId>
  <dependencies>
    ....
  </dependencies>

  <build>
  </build>
</project>

4.0.0
肥罐
com.greg
1.0-快照
藏书罐
....
与库有依赖关系的最终jar,位于同一父项下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>fat-jar</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>final-jar</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>library-jar</artifactId>
            <version>${project.version}</version>
        </dependency>
          ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

4.0.0
肥罐
com.greg
1.0-快照
最后一罐
com.greg
藏书罐
${project.version}
...
org.apache.maven.plugins
maven阴影插件
3.2.1
包裹
阴凉处
com.greg.App
不要使用
系统
,这会让maven认为jar是一个随处可用的系统库。我也遇到了同样的问题,犯了同样的错误,但通过使用
maven shade插件
解决了这个问题,该插件能够创建一个胖罐子。此外,您应该将本地库安装到maven,而不是将其安装在项目中。有关解决方案,请参阅。的可能副本
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>fat-jar</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>final-jar</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>library-jar</artifactId>
            <version>${project.version}</version>
        </dependency>
          ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>