Maven中Pom文件打包配置build

摘自:https://my.oschina.net/simpleton/blog/607776

方法一
<build>
    <finalName>###架包名###</finalName>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <!-- 控制资源文件的拷贝 -->
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}</targetPath>
            <!-- excludes和includes二选一使用即可 -->
            <!-- 不包含的文件,支持通配符 -->
            <excludes>
                <exclude>*.txt</exclude>
            </excludes>
            <!-- 包含的文件,支持通配符 -->
            <includes>
                <include>*.properties</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <!-- 设置源文件编码方式 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.eya.main.Hello</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <!-- 拷贝依赖的jar包到lib目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 解决资源文件的编码问题 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- 打包source文件为jar文件(源码,可选) -->
        <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <attach>true</attach>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
        <profile>
            <id>A</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>B</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>
方法二:
<build>
        <resources>
            <resource>
                <targetPath>${project.build.directory}/classes</targetPath>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.test.testguava.app.App</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>applicationContext.xml</resource>
                                </transformer>
                            </transformers>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>executable</shadedClassifierName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>A</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>B</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <build>
                <filters>
                    <filter>路径/application-环境名.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>

方法三

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>test</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>develop</id>
            <properties>
                <build.profile.id>develop</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>formal</id>
            <properties>
                <build.profile.id>formal</build.profile.id>
            </properties>
        </profile>
    </profiles>

    <build>
        <filters>
            <filter>src/app-${build.profile.id}.properties</filter>
        </filters>
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- 定义打包资源文件-->
        <resources>
            <resource>
                <filtering>true</filtering>
                <!--加上filter会过滤该资源路径中的文件-->
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.***.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
Maven POM (Project Object Model) 文件Maven 项目的核心配置文件,其中包含了项目中所有的依赖、插件、构建配置等信息。在 Maven 打包过程中,POM 文件的正确配置非常重要。以下是一个基本的 Maven POM 文件配置示例: ```xml <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>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <!-- 依赖声明 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- 插件声明 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> ``` 在上述 POM 文件中,`<groupId>`、`<artifactId>` 和 `<version>` 标签定义了项目的基本信息。`<dependencies>` 标签用于声明项目所依赖的其他库,`<build>` 标签下的 `<plugins>` 标签用于声明项目构建时所需要的插件。 在打包过程中,可以使用以下命令: ``` mvn package ``` 该命令会在当前项目根目录下生成一个名为 `my-app-1.0-SNAPSHOT.jar` 的可执行 JAR 文件,其中包含了项目的所有依赖和代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值