Maven 打包 Java 项目

既然要使用Maven命令打包,首先需要先在项目的pom.xml文件中配置Maven插件:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>公司组织的ID</groupId>
    <artifactId>项目名</artifactId>
    <version>3.7.8</version>
    <!-- 这里设定打包格式是war包, 如果需要jar包则写jar -->
    <packaging>war</packaging>
    ...

    <build>
        <plugins>
        	<!-- Spring项目需要增加的插件 -->
			<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!--Spring Boot项目的插件如下-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

上面列了Spring项目和Spring Boot项目的插件,根据项目选择对应的插件配置即可。

接着打开命令行窗口,进入项目所在的目录,然后就可以使用Maven命令进行打包了

在这里插入图片描述
使用的命令是:

mvn clean package [-D] [-U] [-P]

这些参数的说明如下:

  • clean:清理之前旧的打包项目
  • package:打包项目
  • -D:指定参数,如 -Dmaven.test.skip=true 为跳过编译单元测试代码
  • -U:强制从远程上拉取 snapshot 的依赖和依赖
  • -P:指定profile配置,用于区分环境。因为项目一般会分开发版本(dev), 测试版本(test)和正式生产版本(prod),有时候只是打包测试版本发给测试,则加上 -Ptest 就能打包相应配置的包,各种配置版本可以在 pom.xml 中增加:
<profiles>
     <profile>
         <id>dev</id>
         <properties>
             <profile.type>dev</profile.type>
             <jdbc.url>jdbc:mysql://127.0.0.1:3306/scott?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;connectionCollation=utf8mb4_bin</jdbc.url>
             <jdbc.username>root</jdbc.username>
             <jdbc.password>123456</jdbc.password>   
             ...
         </properties>
         <activation>
             <activeByDefault>true</activeByDefault>
         </activation>
     </profile>
</profiles>

如果是使用 IntelliJ IDEA 开发的话,可以在 configurations 中配置打包命令:
在这里插入图片描述在这里插入图片描述
配置好以后就可以直接运行配置来打包:
在这里插入图片描述
打包完会自动保存在项目文件夹下的 target 文件夹中:
在这里插入图片描述

发布了14 篇原创文章 · 获赞 0 · 访问量 740

猜你喜欢

转载自blog.csdn.net/JasonWeng/article/details/105442752