zl程序教程

您现在的位置是:首页 >  Java

当前栏目

通过idea打包java Maven项目 架包与全包

2023-02-25 18:19:50 时间

??????养成每日阅读好习惯, 每天进步, 超越昨天的自己?????? 作者:有勇气的牛排??? 愿景:输出体系化编程知识与技巧,助力软件行业发展与从业者学习减负,让编程产生更大价值。

很多小伙伴在学习Java的过程中,可能对Java Maven打包项目方法有点小迷惑,毕竟Java知识体系庞大,全背下来肯定是有点难度的,今天牛排在学习大数据的时候总结出了使用流程,再也不用背代码了,特此分享给大家。

有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!

1 仅架包

架包定义:指仅将代码打包到jar中,在运行的平台必须保证依赖。

方法:maven —> Lifecyle —> Clean —> Package

image.png

2 架包与全包(推荐)

全包定义:将maven项目中的依赖于代码都打为一个包。

方法:maven —> Plugins —> assembly —>assembly:assembly

项目配置

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>azkaban_cs</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- 引入打包插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!--    <dependencies>-->

    <!--        &lt;!&ndash;Lombok &ndash;&gt;-->
    <!--        <dependency>-->
    <!--            <groupId>org.projectlombok</groupId>-->
    <!--            <artifactId>lombok</artifactId>-->
    <!--        </dependency>-->

    <!--    </dependencies>-->


</project>

新建主目录文件:assembly.xml

<assembly xmlns:xsi="" xmlns="" xsi:schemaLocation="   ">
    <id>${project.version}</id>
    <formats>
        <format>dir</format>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <baseDirectory>${build.directory}</baseDirectory>

    <fileSets>
        <fileSet>
            <directory>conf/</directory>
            <outputDirectory>${project.artifactId}/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>db/</directory>
            <outputDirectory>${project.artifactId}/db</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${build.directory}</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <excludes>
                <exclude>*sources.jar</exclude>
            </excludes>
            <outputDirectory>${project.artifactId}/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>bin/</directory>
            <outputDirectory>${project.artifactId}/bin</outputDirectory>
            <fileMode>754</fileMode>
            <lineEnding>unix</lineEnding>
        </fileSet>
    </fileSets>
</assembly>

开始打包

image.png