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

相关推荐
柠檬Leade2 小时前
IDEA中 java: 程序包lombok不存在 问题解决
java·开发语言·maven·intellij-idea·依赖不存在
非凡的小笨鱼2 小时前
IDEA找不到类编译不通过的解决方案
java·maven·intellij-idea
番茄去哪了3 小时前
从0到1独立开发一个论坛项目(一)
java·数据库·oracle·maven
攒了一袋星辰19 小时前
SequenceGenerator高并发有序顺序号生成中间件 - 架构设计文档
java·后端·spring·中间件·架构·kafka·maven
spencer_tseng1 天前
ojdbc6-1.0.0.jar xmlworker-1.0.0.jar
java·maven·jar
朱一头zcy1 天前
[IDEA不同版本中]配置完Maven后 重启/导入新项目就恢复默认配置(C盘.m2)的解决方案
经验分享·maven·intellij-idea
麦麦鸡腿堡1 天前
JavaWeb_maven
java·开发语言·maven
不吃香菜学java2 天前
苍穹外卖-新增菜品需求分析
java·spring boot·spring·tomcat·maven·ssm
smile_life_2 天前
使用idea查看maven依赖
java·maven·intellij-idea