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

相关推荐
逍遥德3 天前
Maven教程.02-基础-pom.xml 使用标签大全
java·后端·maven·软件构建
逍遥德3 天前
Maven教程.01- settings.xml 文件<profile>使用详解
xml·java·maven
逍遥德3 天前
Maven教程.04-如何阅读Maven项目
java·maven
逍遥德3 天前
Maven教程.03-如何阅读pom.xml文件
xml·java·后端·maven
塔中妖3 天前
Windows 安装 Maven 详细教程(含镜像与本地仓库配置)
java·windows·maven
成为你的宁宁3 天前
Jenkins 自动化部署前后端分离若依项目全攻略:涵盖环境配置、Maven/Node.js 工具安装、GitLab 项目协同,及前后端构建、服务器推送与代码更新验证全步骤
node.js·自动化·gitlab·jenkins·maven
没有bug.的程序员4 天前
依赖治理之巅:Maven 与 Gradle 依赖树分析内核、冲突判定博弈与工程自愈实战指南
java·gradle·maven·依赖治理·冲突判定·依赖树
三无少女指南4 天前
Spring Boot项目中Maven编译参数source、target与release的区别及配置实践
java·spring boot·maven
七夜zippoe4 天前
微服务架构下Spring Session与Redis分布式会话实战全解析
java·redis·maven·spring session·分布式会话
何中应5 天前
从零搭建Maven私服(Nexus)
java·运维·maven