简体   繁体   English

在 Intelij 中为 Maven 项目创建运行配置?

[英]Creating a Run Configuration for Maven Project in Intelij?

I have a simple standard Maven project that I have created using Intelij .我有一个使用Intelij创建的简单标准Maven项目。

Pom.xml: Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>MavenExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.15.Final</version>
        </dependency>

    </dependencies>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.rs.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Main class:主class:

public class App {

    public static void main(String[] args) {

        System.out.println("Hello all!");
    }
}

Currently I run this project by first:目前我首先运行这个项目:

mvn clean install

then navigate to target dir and run:然后导航到目标目录并运行:

java -jar MavenExample-1.0-SNAPSHOT.jar

How can I create a run configuration in intelij to run this project without needing to manually run the jar command?如何在intelij中创建运行配置来运行此项目,而无需手动运行 jar 命令?

There are multiple ways by which we can run our Java application.我们可以通过多种方式运行 Java 应用程序。 The commonly used ones are either from the command line or from the IDE itself.常用的要么来自命令行,要么来自 IDE 本身。

From command line从命令行

  1. java -jar command. java -jar命令。

    Example: java -jar jar-name.jar示例: java -jar jar-name.jar

  2. java -cp command. java -cp命令。

    Example: java -cp jar-name.jar com.package.ClassName示例: java -cp jar-name.jar com.package.ClassName

where com.package.ClassName is the class that contains the main method to run the program.其中com.package.ClassName是包含运行程序的主要方法的 class。 cp in the above command stands for the classpath.上面命令中的 cp 代表类路径。

From IDE从 IDE

Simply do the right-click in the class and you will be able to find the option to run your code.只需在 class 中单击鼠标右键,您就可以找到运行代码的选项。 Just click on that option.只需单击该选项。 Check this image for detail.检查此图像以获取详细信息。

运行主方法

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

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