一个是生产打包的时候, 一个是本地测试启动的时候,maven如何配置?

在Maven项目中,使用两套不同的pom.xml配置分别用于生产打包和本地测试启动是常见需求,尤其当你需要调整依赖范围、插件配置或使用不同资源文件时。Maven通过profiles和activeProfiles提供了灵活的配置管理方案,允许你为不同的环境或构建场景定义特定的配置集。

下面是如何利用Maven profiles来满足你的需求:

1. 定义 Profiles

pom.xml文件中添加<profiles>标签,并在其中定义两个profiles,分别命名为productiontest

xml 复制代码
<project>
    ...
    <profiles>
        <profile>
            <id>production</id>
            <!-- 生产环境配置 -->
            <dependencies>
                <!-- 生产环境特有的依赖 -->
                <dependency>
                    <groupId>...</groupId>
                    <artifactId>...</artifactId>
                    <version>...</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <!-- 生产环境特有的插件配置 -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib/</classpathPrefix>
                                    <mainClass>com.example.MainClass</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>test</id>
            <!-- 测试环境配置 -->
            <dependencies>
                <!-- 测试环境特有的依赖 -->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.12</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <!-- 测试环境特有的插件配置 -->
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    ...
</project>

2. 指定 Active Profiles

要激活某个profile,你可以在Maven命令行中使用-P参数来指定,例如:

  • 激活生产环境配置

    bash 复制代码
    mvn clean package -Pproduction
  • 激活测试环境配置

    bash 复制代码
    mvn clean test -Ptest

3. 默认 Profile

如果你希望在没有指定-P参数时,默认激活某个profile,可以在<profiles>标签中使用<activation>元素:

xml 复制代码
<profiles>
    <profile>
        <id>default</id>
        <!-- 默认配置 -->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <!-- 默认配置的内容 -->
    </profile>
    <!-- 其他 profiles -->
</profiles>

这样,如果没有显式激活其他profiles,default profile将被自动激活。

4. 使用条件激活

你还可以根据环境变量、操作系统或其他条件来自动激活某个profile:

xml 复制代码
<profile>
    <id>production</id>
    <!-- 生产环境配置 -->
    <activation>
        <property>
            <name>env</name>
            <value>prod</value>
        </property>
    </activation>
</profile>

以上配置表示,当环境变量env的值为prod时,production profile将被激活。

通过这种方式,你可以轻松地在不同的构建场景下切换Maven的配置,确保生产打包和本地测试启动时使用最合适的依赖和插件设置。

相关推荐
Derek_Smart27 分钟前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP1 小时前
MyBatis-mybatis入门与增删改查
java
孟陬5 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌5 小时前
一站式了解四种限流算法
java·后端·go
华仔啊5 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝6 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
Nyarlathotep01137 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen57 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java
SimonKing7 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员