Intellij IDEA Maven Plugin - Manage Dependencies

2019-09-06 18:52发布

I'm new to IntelliJ and Maven and tried a bit around. I imported some libraries via the Module & Project Structure Settings in the "Project Structure" window (see screenshot below), because I thought that I have to add my dependencies here. I also thought that this creates the pom.xml with dependency list automatically, but it didn't! I had to add my dependencies via following steps:

Open the pom.xml file > Menu "Code" > "Generate" > Popup "Dependency"

or by

Alt + Insert

Project Structure Window:

enter image description here

To export all my libraries I also had to add a plugin in the pom.xml:

<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>your.package.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>

       [...]

    </dependency>
</dependencies>

and set the goal via run configuration to

clean compile assembly:single package

This created me on running two jars:

test-1.0-SNAPSHOT.jar & test-1.0-SNAPSHOT-jar-with-dependencies.jar

That was a lot of things to do for a beginner! My question now: Is there a shorter way to manage the project's dependencies? And what is the project structure setting really for (in case of dependency management)??? Did I do too much or something wrong?

PS: For all who are new to Maven & IntelliJ, see https://www.jetbrains.com/idea/help/maven.html

1条回答
不美不萌又怎样
2楼-- · 2019-09-06 19:36

If you use maven you just have to add your dependencies on the pom.xml.

You also can use

Open the pom.xml file > Menu "Code" > "Generate" > Popup "Dependency"

As you do. InteliJ will add it to the classpath of the your project automatically.

Project Structure is used when you have submodule for example. It's not here to add jar.

查看更多
登录 后发表回答