简体   繁体   中英

Creating a Run Configuration for Maven Project in Intelij?

I have a simple standard Maven project that I have created using Intelij .

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:

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?

There are multiple ways by which we can run our Java application. The commonly used ones are either from the command line or from the IDE itself.

From command line

  1. java -jar command.

    Example: java -jar jar-name.jar

  2. java -cp command.

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

where com.package.ClassName is the class that contains the main method to run the program. cp in the above command stands for the classpath.

From IDE

Simply do the right-click in the class and you will be able to find the option to run your code. Just click on that option. Check this image for detail.

运行主方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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