欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven打包,不包含依赖jar包

程序员文章站 2022-04-28 19:11:04
...

方式1

pom 配置

<properties>
    <startClass>com.answer.jaemon.Application</startClass>
</properties>

<build>
    <plugins>
        <!-- 生成不包含依赖jar的可执行jar包 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                	<!--
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    -->
                    <configuration>
                        <!-- 不加的话最终包名为: ${artifactId}-${version}.jar, 加了的话最终包名: ${artifactId}-${version}-${classifier}.jar  -->
                        <classifier>execute</classifier>
                        <!-- 不指定生成路径的话, 默认保存在 ${build.directory} 下 -->
                        <outputDirectory>
                            ${project.build.directory}/execute
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <finalName>${artifactId}-${version}</finalName>
                <layout>ZIP</layout>
                <mainClass>${startClass}</mainClass>
                <includes>
                    <include>
                        <groupId>bframe-parent</groupId>
                        <artifactId>bframe-aut-dao</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>

        <!-- 拷贝依赖的jar包到lib目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
                        <excludeTransitive>false</excludeTransitive>
                        <!-- 复制的jar文件去掉版本信息 -->
                        <stripVersion>true</stripVersion>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 设置源文件编码方式 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <!-- 解决资源文件的编码问题 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

 

打包命令

mvn clean install -Dmaven.test.skip=true

 

方式2

mvn dependency:tree dependency:copy-dependencies -DoutputDirectory=.\target\lib -DincludeScope=runtime

 

启动jar包服务

java -Dloader.path=./lib -jar execute\jaemon-system-1.0-execute.jar
java -Dloader.path=./lib -jar execute\jaemon-system-1.0-execute.jar --spring.profiles.active=dev