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文件中。

相关推荐
記億揺晃着的那天6 小时前
Amazon Ads API 自建 SDK 架构升级实战:从手工生成 JAR 到可追溯构建体系
maven·架构设计·sdk开发·openapi·amazon ads api
小白的码BUG之路14 小时前
Jenkins -- 配置jdk和maven
java·jenkins·maven
砚底藏山河16 小时前
数据新鲜度 SLA 监控
java·大数据·python·maven
开开心心就好2 天前
手机电池健康怎么查?看容量温度循环次数
spring·智能手机·ffmpeg·计算机外设·电脑·maven·音视频
知守观2 天前
mirrorOf= 把私服请求拦去了公网镜像:本地能跑,CI 上内部包 404 排查
后端·ci/cd·maven
不老刘2 天前
Maven 多医院分支场景下 groupId 的设计与 Jenkins 落地实践
servlet·jenkins·maven
毕业设计7032 天前
(免费领源码)基于node的学生公寓管理系统06412- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·mysql·node.js·maven·mybatis·express·b/s
知守观3 天前
同事的 SNAPSHOT 更新了,我三天后才拉到:Maven 更新检查被 _remote.repositories 静默跳过
java·后端·maven
知守观3 天前
Maven SNAPSHOT 更新排查:-U 无效、clean 无用,问题在 _remote.repositories(附完整链路)
java·后端·maven
知守观4 天前
多模块工程没 install,跑起来的代码是上周的:Maven reactor 依赖解析踩坑
java·maven·intellij idea