简体   繁体   English

直接从 Maven 存储库执行 JAR 文件

[英]Executing a JAR file straight from a Maven repository

Suppose that we have a Java application , rather than a library, which is available through Maven central.假设我们有一个 Java应用程序,而不是一个库,它可以通过 Maven 中心获得。 For example, one such project is jol , which has its corresponding CLI interface in Maven central .例如,一个这样的项目是jol ,它在 Maven central 中有其对应的 CLI 接口。

As far as I can tell, the main difference from a library is that the corresponding JAR file contains a class with an appropriate main() method and, optionally, a related Main-Class: header in the JAR manifest.据我所知,与库的主要区别在于相应的 JAR 文件包含一个具有适当main()方法的类,以及可选的 JAR 清单中的相关Main-Class:标头。 If such an artifact is used as a dependency on a project, Maven will happily download the JAR file to the local repository along with its dependencies, as it does for any other artifact.如果这样的工件被用作项目的依赖项,Maven 会很高兴地将 JAR 文件连同它的依赖项一起下载到本地存储库,就像它为任何其他工件所做的那样。

Is it possible to use Maven to execute such an application directly, without setting up a Maven project?是否可以使用Maven直接执行这样的应用程序,而无需设置Maven项目?

Theexec:java plugin works nicely for local projects by setting up the JVM classpath so that dependencies are available. exec:java插件通过设置 JVM 类路径使依赖项可用,从而很好地适用于本地项目。 The user does not have to worry about JAR or .class file locations and such.用户不必担心 JAR 或.class文件位置等。 Unfortunately, from what I can tell, it also requires an enclosing Maven project, so it cannot be used from an arbitrary command line prompt.不幸的是,据我所知,它还需要一个封闭的 Maven 项目,因此不能在任意命令行提示符下使用它。

No, Maven will not do what you are asking for.不,Maven 不会做你所要求的。 It is a build tool, intended to build a Java project based on it's pom.xml file which describes the project.它是一个构建工具,旨在基于描述项目的 pom.xml 文件构建 Java 项目。

So, you can't run a maven build without a pom.xml file.因此,如果没有 pom.xml 文件,您将无法运行 Maven 构建。 And if you have a pom.xml, then by definition, you have 'set up a Maven project'.如果你有一个 pom.xml,那么根据定义,你已经“建立了一个 Maven 项目”。

As @DaveNewton says, you should be able to set up a very small pom.xml with the dependency for the jar file in question, and the exec-maven plugin.正如@DaveNewton 所说,您应该能够设置一个非常小的 pom.xml,其中包含相关 jar 文件的依赖项和 exec-maven 插件。 I'm afraid that it's just not going to get any simpler than that.我担心它不会变得比这更简单。

A hacky solution working with Maven 3 would be to use the Maven Dependency Plugin in combination with the Maven Help Plugin to resolve the local repository path:使用 Maven 3 的一个hacky 解决方案是将 Maven Dependency Plugin 与 Maven Help Plugin 结合使用来解析本地存储库路径:

# Download JAR from Maven repo
mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ \
                   -DgroupId=some.group.id \
                   -DartifactId=some-artifact-cli \
                   -Dversion=1.0.0 \
                   -Dtransitive=false

# Resolve local repository path
MVN_REPO=$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)

# Execute JAR from the local repository
java -jar $MVN_REPO/some/group/id/some-artifact-cli/1.0.0/some-artifact-cli-1.0.0.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM