简体   繁体   English

如何运行由Maven编译的程序?

[英]How can I run a program compiled by Maven?

I am starting to learn Maven by reading https://spring.io/guides/gs/maven/ . 我通过阅读https://spring.io/guides/gs/maven/开始学习Maven。

In the examples, after running mvn compile successfully, how can I run the program via maven? 在示例中,成功运行mvn compile后,如何通过maven运行程序? This part seems missing from the article. 该部分似乎缺少本文。

Thanks. 谢谢。

You can invoke a Java program (ie with a public static void main(String[] args) signature) with the classpath of the combined dependencies for the current pom.xml using 您可以使用当前pom.xml的组合依赖项的类路径调用Java程序(即,使用public static void main(String[] args)签名)。

mvn -q exec:java

You need to configure the main class to invoke in your pom.xml similar to 您需要配置主类以在pom.xml中调用,类似于

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>demo.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

This is useful for testing and development, but not deployment 这对于测试和开发很有用,但对部署没有帮助

See http://www.mojohaus.org/exec-maven-plugin/usage.html for full details. 有关完整的详细信息,请参见http://www.mojohaus.org/exec-maven-plugin/usage.html

Generally, maven is not used for running code. 通常,maven不用于运行代码。 This is a build tool that you can use for compiling, running unit or integration tests, deploying you your code locally and remotely, etc.. 这是一个构建工具,可用于编译,运行单元或集成测试,在本地和远程部署代码等。

It is based around the idea of a build lifecycle where which is in its turn is defined by a list of build phases. 它基于构建生命周期的思想,而构建生命周期又由一系列构建阶段定义。 For example, the default lifecycle has the following phases: 例如,默认生命周期具有以下阶段:

  • validate - validate the project is correct and all necessary information is available 验证 -验证项目是否正确并且所有必要的信息均可用
  • compile - compile the source code of the project 编译 -编译项目的源代码
  • test - test the compiled source code using a suitable unit testing framework. 测试 -使用合适的单元测试框架测试已编译的源代码。 These tests should not require the code be packaged or deployed 这些测试不应要求将代码打包或部署
  • package - take the compiled code and package it in its distributable format, such as a JAR. package-打包已编译的代码,并将其打包为可分发的格式,例如JAR。
  • verify - run any checks on results of integration tests to ensure quality criteria are met 验证 -对集成测试的结果进行任何检查,以确保符合质量标准
  • install - install the package into the local repository, for use as a dependency in other projects locally install-将软件包安装到本地存储库中,以作为本地其他项目中的依赖项
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects. 部署 -在构建环境中完成,将最终软件包复制到远程存储库,以便与其他开发人员和项目共享。

For more information you can refer to this . 有关更多信息,您可以参考

UPDATE : 更新

Having said that, it is possible as mentioned in Thorbjørn Ravn Andersen answer here. 话虽如此,有可能在ThorbjørnRavn Andersen的答复中提到。

The Maven build process has a number of lifecycles, or points in the process. Maven构建过程具有多个生命周期或过程中的各个点。 compile is one of the build steps, but most likely running the following would resolve your issue: compile是构建步骤之一,但是很可能运行以下命令可以解决您的问题:

mvn clean package

This would generate a JAR file, in the folder where you ran it. 这将在运行它的文件夹中生成一个JAR文件。 You can then try running this JAR file using java . 然后,您可以尝试使用java运行此JAR文件。

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

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