Intellij IDEA Maven Plugin - Manage Dependencies

試著忘記壹切 提交于 2020-01-07 03:03:09

问题


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:

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:


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.



来源:https://stackoverflow.com/questions/34088277/intellij-idea-maven-plugin-manage-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!