Maven编译打包

Maven

依赖范围(scope)

  • 依赖范围:scope

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <!-- 依赖范围 -->
        <scope>compile</scope>
    </dependency>
    
  • 可选值:compile/test/provided/system/runtime/import

    main目录test目录开发运行打包运行
    compile有效有效有效有效
    test无效有效有效无效
    provided有效有效有效无效
    system
    runtime
    import

plugin

【编译Java源代码】配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <!-- 指定插件的版本 -->
    <version>3.3</version>
    <configuration>
        <!-- 指定源代码的版本 -->
        <source>1.8</source>
        <!-- 指定生成的字节码版本 -->
        <target>1.8</target>
        <!-- 指定源文件的编码 -->
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

【打包】配置

打包成一个可执行的 JAR(maven-shade-plugin)

maven-shade-plugin 官网链接

将 Java 应用程序及其所有依赖项打包成一个可执行的 JAR 文件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <!-- 将 Java 应用程序及其所有依赖项打包成一个可执行的 JAR 文件 -->
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
        <!-- 不创建依赖减少的 POM 文件 -->
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <artifactSet>
            <excludes>
                <!-- 排除所有 org.apache.hadoop 的依赖 -->
                <exclude>org.apache.hadoop:*</exclude>
            </excludes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <!-- 在打包阶段执行 -->
            <phase>package</phase>
            <goals>
                <!-- 执行 shade 目标 -->
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <!--如果要打包的话,这里要换成对应的 main class-->
                        <mainClass>com.sensorsdata.analytics.DataPreProcess</mainClass>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <!-- 适用于所有工件的过滤器 -->
                        <artifact>*:*:*:*</artifact>
                        <!-- 排除所有 .SF .DSA .RSA 文件 -->
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

将项目输出合并到单个可分发的包中(maven-assembly-plugin)

maven-assembly-plugin 官网链接

将所有文件打包成一个单独的分发包

两个步骤

  1. 编写 assembly.xml 配置文件(将项目的工件与生成的文档一起打包到压缩包中)
  2. 使用 maven-assembly-plugin 打包插件并引用 assembly.xml 配置文件

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <id>bin</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <!-- 引用 assembly.xml 配置文件 -->
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>bin</id>
    <!-- 压缩格式 -->
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!-- 将 ${project.parent.basedir}/data_export_engine-dist/src/main/bin 目录中的所有文件放到 bin 目录 -->
        <fileSet>
            <!-- 模块目录的绝对或相对位置 -->
            <!-- ${project.parent.basedir} : 当前项目的父项目的基础目录 -->
            <!-- ${project.basedir} : 当前项目的基础目录 -->
            <directory>${project.parent.basedir}/data_export_engine-dist/src/main/bin</directory>
            <!-- 在directory目录中选择需要打包的文件 -->
            <includes>
                <include>**</include>
            </includes>
            <!-- 相对于程序级根目录的输出目录 -->
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <!-- 将 ${project.parent.basedir}/data_export_engine-common/target 目录中的所有 jar包 放到 lib 目录 -->
        <fileSet>
            <directory>${project.parent.basedir}/data_export_engine-common/target</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
    <!-- 将所有的依赖jar包放到 lib 目录(依赖范围:runtime) -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值