pom.xml读本地Maven配置文件

可通过这种方式实现,不把一些敏感信息打入最终jar包,从而不暴露给最终用户。

项目yml文件相关部分:

复制代码
clientId:
  registerEnv: dev

pom.xml中相关部分如下。使用了groovy-maven-plugin 插件,并使用了SnakeYAML来解析yml文件:

xml 复制代码
   <build>
    。。。
     <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                import org.yaml.snakeyaml.Yaml
                                import java.io.FileInputStream

                                def targetDir = new File("target")
                                if (!targetDir.exists()) {
                                    targetDir.mkdirs()
                                }

                                // 读取 application.yml中的值:
                                def yamlFile = new File(project.basedir, "src/main/resources/application.yml")
                                def yaml = new Yaml()
                                def config = yaml.load(new FileInputStream(yamlFile))
                                def env = config.clientId?.registerEnv

                                def urlMap = [    // 根据环境选择对应的URL(来自settings.xml)
                                                  "dev" : "${client.dev.url}",
                                                  "prod": "${client.prod.url}"
                                ]
                                def registerUrl = urlMap[env]

                                def response = new URL(registerUrl).text
                                def file = new File(targetDir, "license.txt")
                                file.write(response.trim())
                            </source>
                        </configuration>
                    </execution>
                </executions>
                <!-- SnakeYAML 依赖(用于解析 YAML) -->
                <dependencies>
                    <dependency>
                        <groupId>org.yaml</groupId>
                        <artifactId>snakeyaml</artifactId>
                        <version>2.0</version>
                    </dependency>
                </dependencies>
            </plugin>
            。。。

本地Maven的settings.xml相关部分如下,使用了Maven Profiles:

xml 复制代码
<settings>
。。。
	<profiles>
		<profile>
			<id>hanfu-register-client-url</id>
			<properties>
				<client.dev.url>http://127.0.0.1:8083/hanfu/registerClient</client.dev.url>
				<client.prod.url>http://xxx.xxx.xxx.xxx:25088/hanfu/registerClient</client.prod.url>
			</properties>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>hanfu-register-client-url</activeProfile>
	</activeProfiles>
</settings>

可见Maven Profiles不仅可以配置在软件项目的pom.xml中,如我博:根据OS自动加载不同的native库和本地jar包 ,也可以配置在maven的settings.xml文件中。

相关推荐
我真会写代码1 天前
SSM(指南一)---Maven项目管理从入门到精通|高质量实操指南
java·spring·tomcat·maven·ssm
vx1_Biye_Design1 天前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
qq_336313932 天前
javaweb-maven单元测试
java·开发语言·maven
计算机毕设指导63 天前
基于微信小程序的校园二手交易系统【源码文末联系】
java·spring boot·spring·微信小程序·小程序·tomcat·maven
多多*3 天前
2026年最新 测试开发工程师相关 Linux相关知识点
java·开发语言·javascript·算法·spring·java-ee·maven
lang201509283 天前
Tomcat Maven插件:部署与卸载的架构设计
java·tomcat·maven
lang201509283 天前
Tomcat Maven插件全解析:开发部署一体化
java·tomcat·maven
食指Shaye3 天前
idea突然出现找不到Maven配置的jar
maven·intellij-idea·jar
tb_first3 天前
万字超详细苍穹外卖学习笔记3
java·jvm·笔记·学习·spring·tomcat·maven
1candobetter3 天前
JAVA后端开发——Maven 依赖传递 ≠ Spring 自动装配
java·spring·maven