Maven:如何查询可执行的类路径? [英] Maven: how to query the executable classpath?

查看:116
本文介绍了Maven:如何查询可执行的类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些指定依赖项的maven项目.

I have a maven project with some specified dependencies.

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
</dependency>

如何查询maven来查找其用于这些依赖项的路径,或者应该用于独立执行的类路径?

How can I query maven to find out the path it's using for these dependencies, or the classpath I should use for independent execution?

我的目标是构建一个包装程序,以适当的类路径运行程序.

My goal is to build a wrapper which runs the program with the appropriate classpath.

推荐答案

Maven中提供了几种替代方案:

Several alternatives are available in Maven:

查看Maven依赖插件,尤其是 build-classpath 目标为外部执行用法提供了完整的类路径.在许多选项中,outputFile参数可能会有所帮助.
您无需对其进行配置即可使用,只需运行

Look at the Maven Dependency Plugin, especially the build-classpath goal provides exactly the full classpath for external execution usages. Among many options, The outputFile parameter may be helpful.
You don't need to configure it for usage, just run

mvn dependency:build-classpath

在您的项目上,您将看到类路径作为构建输出的一部分.或者

On your project and you'll see the classpath as part of the build output. Or

mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt

仅将类路径重定向到文件.

To redirect just the classpath to a file.

要构建包装器,您还可以查看

To build a wrapper, you could also look at the copy-dependencies goal, which would copy the required dependencies (jars), including transitive dependencies, to a configured folder (so you don't need hardcoded paths to your local machine).
An example of plugin configuration is available on the official site, here. For instance, the following configuration:

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

将在范围compile中声明的所有依赖项添加到文件夹target/dependencies中.注意:关于链接的官方示例,我添加了<includeScope>runtime</includeScope>配置条目(根据

Would add to the folder target/dependencies all the dependencies declared in scope compile. NOTE: with respect to the linked official example, I added the <includeScope>runtime</includeScope> configuration entry (which will include compile and runtime scoped dependencies, according to documentation and my tests), otherwise it would also include the test scope by default (which is something I believe you would not need at runtime).

或者,您可以使用 Exec Maven插件执行从Maven使用所需的类路径.
官方网站上提供了一个插件配置示例,

Alternatively, you can use the Exec Maven Plugin to execute a main from Maven using the required classpath.
An example of plugin configuration is available on the official site, here.
The following configuration for instance:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>my-execution</id>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.sample.MainApp</mainClass>
    </configuration>
</plugin>

将配置Exec插件以使其通过mvn exec:java主类MainApp运行,显然具有所需的类路径.

Will configure the Exec plugin to run via mvn exec:java the main class MainApp as configured, obviously with the required classpath.

最后, Maven程序插件还提供了构建带有依赖项的可执行jar,如此处所述a>,另一个关于stackoverflow的问题.

Lastly, the Maven Assembly Plugin also provides facilities to build an executable jar with dependencies, as explained here, in another question on stackoverflow.

这篇关于Maven:如何查询可执行的类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆