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

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

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

1. 定义 Profiles

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

<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参数来指定,例如:

  • 激活生产环境配置

    mvn clean package -Pproduction
    
  • 激活测试环境配置

    mvn clean test -Ptest
    

3. 默认 Profile

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

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

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

4. 使用条件激活

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

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

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

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

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java我跟你拼了

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值