【问题标题】:Maven: Simple classpathLayoutType is generating classpath similar to repository classpathLayoutTypeMaven:简单的 classpathLayoutType 正在生成类似于存储库 classpathLayoutType 的类路径
【发布时间】:2018-01-17 11:21:33
【问题描述】:

我有一个 Maven 项目,它执行以下操作:生成 jar 并将其复制到 target/dist 目录中。将所有依赖项 jar 复制到 target/dist/lib 目录中。 dist 文件夹将被分发。生成的 jar 执行时,需要在类路径中执行依赖 jar。当我在 pom.xml 中添加以下代码时,

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
      </manifest>
    </archive>
  </configuration>
</plugin>

在生成的jar文件中,类路径生成如下:

Class-Path: lib/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar lib/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar

显然,这不会起作用,因为类路径不同。我希望类路径如下:

Class-Path: lib/commons-logging-1.1.1.jar lib/gson-2.8.2.jar

我推荐了this page。根据文档,默认情况下 classpathLayoutType 很简单,这就是我所需要的。但是在存储库布局类型中生成的类路径。我什至尝试明确添加以下标签,但没有成功。

<classpathLayoutType>simple</classpathLayoutType>

为什么简单的layoutType会像repository layoutType那样生成classpath?

我已经使用自定义 layoutType 实现了我需要的功能,如下所示:

<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>

我唯一的问题是为什么简单的 layoutType 不起作用?

【问题讨论】:

    标签: java maven maven-jar-plugin


    【解决方案1】:

    您正在使用哪个 MavenArchiver 插件? 3.0.2 有a bug in ManifestConfiguration.class:

     public String getClasspathLayoutType()
     {
        return "simple".equals(this.classpathLayoutType) ? "repository" : this.classpathLayoutType;
      }
    

    如您所见,ClasspathLayoutType 的方法返回了错误的类型。 在 MavenArchiver 3.1.1 版中,该错误已修复。 layouttype 的可能返回值是:

      public static final String CLASSPATH_LAYOUT_TYPE_SIMPLE = "simple";
      public static final String CLASSPATH_LAYOUT_TYPE_REPOSITORY = "repository";
      public static final String CLASSPATH_LAYOUT_TYPE_CUSTOM = "custom";
    

    试试更高的固定版本。

    【讨论】:

    • 谢谢 :) 但是,我已经继续前进,现在我在一个完全不同的领域工作。所以,我无法检查这是否解决了我的问题:) 只是一个问题。它看起来不像一个错误。它看起来像一个故意的代码。他们为什么要那样做?有什么原因吗?
    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2023-04-04
    • 2011-09-19
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多