在Maven中,如何输出使用的类路径?

Ale*_*ird 43 war classpath maven maven-war-plugin

对于我目前的目的,我有一个Maven项目,它创建一个war文件,我想看看它在创建时使用的实际类路径war.有没有办法在单个命令中执行此操作 - 无需编译整个项目?

一个想法是让Maven生成target/classpath.properties文件,然后在那时停止.

Jan*_*sky 72

要在文件中单独获取类路径,您可以:

mvn dependency:build-classpath -Dmdep.outputFile=cp.txt
Run Code Online (Sandbox Code Playgroud)

或者将其添加到POM.XML:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.9</version>
        <executions>
          <execution>
            <id>build-classpath</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>build-classpath</goal>
            </goals>
            <configuration>
              <!-- configure the plugin here -->
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)

来自:http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

  • 默认情况下,`mvn依赖:build-classpath`将输出`test` [dependency scope]的类路径(https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope )包括`system`,`provided`,`compile`,`runtime`和`test`.如果你想获得`compile`或`runtime`范围的类路径.你可以使用`-Dmdep.includeScope = compile` (7认同)
  • @ecerulm 谢谢,这是一个非常有价值的细节。但是`-DincludeScope=compile` 对我有用,而不是`-Dmdep.includeScope`。 (2认同)

Phi*_*ger 14

或者调用"mvn -e -X ...."并检查输出...

  • 调试信息包含每个涉及的生命周期插件的整个类路径(包括其他内容) (4认同)

Dan*_*tov 10

此命令在Mac和Linux上输出类路径:

mvn -q exec:exec -Dexec.executable=echo -Dexec.args="%classpath"
Run Code Online (Sandbox Code Playgroud)

打印结果而不保存到文件中可能很有用,例如,在将结果分配给Bash脚本中的变量时.此解决方案仅在Mac和Linux上运行,但Bash shell脚本也是如此.

在Windows中(例如在BAT文件中),没有echo可执行文件,你需要这样的东西(未经测试):

mvn -q exec:exec -Dexec.executable=cmd -Dexec.args="/c echo %classpath"
Run Code Online (Sandbox Code Playgroud)

或者,您可以java使用类路径执行程序:

mvn -q exec:exec -Dexec.executable=java -Dexec.args="-cp %classpath Main"
Run Code Online (Sandbox Code Playgroud)

或者甚至喜欢(它会自动使用正确的类路径):

mvn -q exec:java -Dexec.mainClass="Main" 
Run Code Online (Sandbox Code Playgroud)

但是,当程序失败时,这两种替代方法都会受到Maven添加错误消息的影响.

  • 这样做的另一个优点是它还包括项目的类文件(“目标/类”),而不仅仅是依赖项。 (3认同)
  • 它输出类路径而不是将其保存到文件中. (2认同)

Pau*_*ivu 6

该命令mvn dependency:list将列出包含用于编译,运行时和测试的所有jar的类路径,格式如下:

INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ MyProject ---
[INFO]
[INFO] The following files have been resolved:
[INFO]    org.netbeans.api:org-openide-filesystems:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-netbeans-modules-queries:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-netbeans-api-progress:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-openide-dialogs:jar:RELEASE80:compile
[INFO]    org.apache.derby:derby:jar:10.11.1.1:compile
[INFO]    org.netbeans.api:org-openide-windows:jar:RELEASE80:compile
Run Code Online (Sandbox Code Playgroud)

唯一的要求是编译完成.如果未运行编译,则不起作用.

另一个命令是The命令mvn dependency:tree.


mom*_*omo 6

将缺少的范围参数添加到 Janiks 答案中。现在已经完成了。不客气。

mvn -q exec:exec -Dexec.classpathScope="compile" -Dexec.executable="echo" -Dexec.args="%classpath" 
Run Code Online (Sandbox Code Playgroud)


Pav*_* S. 5

正如ecerulm在他对Janik 的回答的评论中指出的那样,您可能希望将范围指定为dependency:build-classpath,因为不同范围的类路径输出会有所不同(默认情况下test出于某种原因使用)。我最终得到了这样的命令:

mvn -DincludeScope=compile dependency:build-classpath
Run Code Online (Sandbox Code Playgroud)

在 POM 中,它可以像这样使用:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.9</version>
        <executions>
          <execution>
            <id>build-classpath</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>build-classpath</goal>
            </goals>
            <configuration>
              <includeScope>compile</includeScope>
              <!-- Omit to print on console: -->
              <outputFile>${project.build.directory}/compile-classpath.txt</outputFile>
            </configuration>
          </execution>
          <execution>
            <id>build-test-classpath</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>build-classpath</goal>
            </goals>
            <configuration>
              <includeScope>test</includeScope>
              <!-- Omit to print on console: -->
              <outputFile>${project.build.directory}/test-classpath.txt</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)

这将输出 2 个版本的类路径,一个用于主构建,另一个用于测试。