现在的位置: 首页 > 综合 > 正文

Maven使用遇到的问题总结

2018年11月07日 ⁄ 综合 ⁄ 共 1679字 ⁄ 字号 评论关闭

在实习过程中使用Maven管理项目和依赖包中遇到的一些问题,特以此文汇总:

Maven工程打成Jar包

最重要的是将依赖打入jar包,由于Maven管理了所有的依赖,所以将项目的代码和依赖打成一个包对它来说是顺理成章的功能。Maven的这个功能之前就用过,用maven的assembly插件,但assembly插件功能强大,可以打zip、war各种包,所以一下子找不到如何将依赖打入jar包了。浪费了一点时间,所以一定要记录一下。 

需要在pom.xml中配置一下:

<build>
<plugins>
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.3</version>
 <executions>
        <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <encoding>UTF-8</encoding>
                <outputDirectory>${project.build.directory}/classes</outputDirectory><!-- 把配置文件拷到和jar包同一个路径下 -->
                <resources>
                    <resource>
                        <directory>src/main/resources/</directory>
                    </resource>
                    <resource>
                        <directory>src/main/java/</directory>
                        <includes>
                        <span style="white-space:pre">	</span><include>**/*.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
</plugins>
</build>

之后再在cmd.exe中cd 到工程路径,分别输入mvn clean,mvn install工程打成的jar包就在Maven工程的target文件夹中,其它非Maven引用即可。

抱歉!评论已关闭.