有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

POM中指定的java依赖项未在正确目录中生成的jar中解析

我想为我的maven应用程序创建一个JAR文件。因此,为了捆绑依赖关系,我使用了maven assembly插件。但是我希望依赖项应该放在lib/文件夹中,而不是放在其他任何地方。我现在的pom。xml插件标记是

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

请建议。同样,maven jar插件也可以实现同样的效果

在POM中进行上述更改之后,依赖项在jar中生成,该jar使用后缀“jar with dependencies”生成。但是依赖JAR是分散的,我希望所有这些依赖JAR都是一个文件夹库的一部分,类似于war文件的WEB-INF/lib。我如何做到这一点


共 (1) 个答案

  1. # 1 楼答案

    您可以使用如下自定义程序集描述符(xml文件)实现这一点:

    <assembly
       xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    
      <id>dist</id>
    
      <formats>
        <format>dir</format>
      </formats>
    
      <fileSets>
        <fileSet>
          <directory>target</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>your-application.jar</include>
          </includes>
        </fileSet>
      </fileSets>
    
      <dependencySets>
        <dependencySet>
          <outputDirectory>lib</outputDirectory>
        </dependencySet>
      </dependencySets>
    
    </assembly>
    

    您必须指向maven assembly插件配置中的程序集描述符:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.4.1</version>
      <configuration>
        <descriptors>
          <descriptor>assemble.xml</descriptor>
        </descriptors>
      </configuration>
      ...
    </plugin>
    

    希望有帮助