【问题标题】:Maven - Export Classpath based on dependencies without .m2 overheadMaven - 基于依赖项导出类路径,无需 .m2 开销
【发布时间】:2019-03-20 16:46:22
【问题描述】:

下面底部的 pom 代码创建了一个类路径文件,其中包含与 .m2 maven 缓存相关的所有 jar 文件:

${M2_REPO}\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;${M2_REPO}\com\github\jnr\jnr-ffi\2.1.7\jnr-ffi-2.1.7.jar;...

所有依赖都被整齐地复制到 target/lib

${project.build.directory}/lib

我想知道如何让 maven 使用我的 target/lib 目录的路径而不是 maven 缓存来创建类路径:

目标/libslf4j-api-1.7.25.jar:目标/lib/jnr-ffi-2.1.7.jar:...

我正在使用以下 maven 代码:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>false</overWriteSnapshots>
          <overWriteIfNewer>true</overWriteIfNewer>
        </configuration>
      </execution>
      <execution>
        <id>build-classpath</id>
        <phase>package</phase>
        <goals>
          <goal>build-classpath</goal>
        </goals>
        <configuration>
          <attach>true</attach>
          <outputFile>${project.build.directory}/classpath</outputFile>
        </configuration>
      </execution>
    </executions>
    </plugin>

【问题讨论】:

  • 使用target有什么好处?
  • 我想要一个配置来构建我的项目并将所有依赖项安排在一个目标目录中。目前这就是 target/myproject.jar 和 target/lib。那部分正在工作。我想念的是一种拥有类路径或清单的方法,其中包括我在目标/库中的所有依赖项并以正确的顺序。 build-path 为我提供了正确的 jar 顺序,但它指的是 .m2-Maven 目录。现在,每次依赖项更改时,我都必须手动编辑类路径并更新我的启动脚本。
  • 谢谢,我明白你想要什么,但我想知道为什么你想要这个。你想通过在目标中构建一个包含条目的类路径来解决什么问题(不能用“通常的”类路径解决)?
  • 您正在构建什么样的项目?战争/耳朵?

标签: maven classpath


【解决方案1】:

以下内容为我完成了这项工作:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}${file.separator}lib</outputDirectory>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>false</overWriteSnapshots>
          <overWriteIfNewer>true</overWriteIfNewer>
        </configuration>
      </execution>
      <execution>
        <id>build-classpath</id>
        <phase>package</phase>
        <goals>
          <goal>build-classpath</goal>
        </goals>
        <configuration>
          <attach>true</attach>
          <outputFile>${project.build.directory}${file.separator}classpath</outputFile>
          <prefix>target${file.separator}lib</prefix>
        </configuration>
      </execution>
    </executions>
  </plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2012-02-15
    • 2013-11-25
    • 1970-01-01
    相关资源
    最近更新 更多