学习autoconfig

仅作个人学习时,记录自己遇到的问题所用,不喜勿喷。

对autoconfig的介绍详见:http://www.openwebx.org/docs/autoconfig.html

感谢:http://www.iteye.com/topic/1014149

这位仁兄的博客讲的更细致,更专业


今天学习了如何在maven中使用autoconfig

首先,创建maven项目,java项目即可

创建一个java类

public class TestAutoConfig {

    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(TestAutoConfig.class.getClassLoader().getResourceAsStream("test.properties"));
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("age"));
        System.out.println(properties.getProperty("password"));
    }
}

在src/main/resources目录下创建配置文件test.properties

name=zhangsan
age=20
password=111111

同目录下创建文件test.properties.vm

name=${name}
age=${age}
password=${password}

在src/main/resources目录下创建META-INF/autoconf/antoconfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<group>
		<property name="name" description="姓名" />
		<property name="password" description="密码" />
		<property name="age" description="年龄" >
			<validator name="number" />
		</property>
	</group>
	<script>
		<generate template="test.properties.vm" destfile="test.properties" />
	</script>
</config>
pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zhang</groupId>
	<artifactId>testAutoConfig</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>testAutoConfig</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- 定义autoconfig的版本,建议将此行写在parent pom.xml中。 -->
		<autoconfig-plugin-version>1.2</autoconfig-plugin-version>
	</properties>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
                        <!-- 执行java -jar命令时,出错:jar没有主清单属性,查找http://www.iteye.com/topic/1014149,发现这个配置 -->
              <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 打出的jar包中的 MANIFEST.MF 文件中增加 Main-Class 这一项配置,这样就能在命令行中通过 java 
                            -jar 来执行打出的jar包 -->
                        <manifestEntries>
                            <Main-Class>com.zhang.TestAutoConfig</Main-Class>
                        </manifestEntries>
                    </archive>
                </configuration>
              </plugin>

			<plugin>
				<groupId>com.alibaba.citrus.tool</groupId>
				<artifactId>autoconfig-maven-plugin</artifactId>
				<version>${autoconfig-plugin-version}</version>
				<configuration>
					<!-- 要进行AutoConfig的目标文件,默认为${project.artifact.file}。 <dest>${project.artifact.file}</dest> -->
					<!-- 配置后,是否展开目标文件,默认为false,不展开。 <exploding>true</exploding> -->
					<!-- 展开到指定目录,默认为${project.build.directory}/${project.build.finalName}。 
						<explodedDirectory> ${project.build.directory}/${project.build.finalName} 
						</explodedDirectory> -->
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>autoconfig</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
然后到项目根目录,执行

mvn install
第一次运行需要在C:\Users\user目录下创建antx.properties文件,只需要在命令行根据提示填写auto-config.xml文件中定义的三个property的值即可,填写的值就是我们想将配置文件中的值修改后的值

这个时候可能遇到错误:

duplicate entry: META-INF/maven/com.zhang/test-autoconfig/pom.xml
duplicate entry: META-INF/maven/com.zhang/test-autoconfig/pom.properties
到targat/class目录,找到相应文件,删除即可

然后到项目targat目录执行

java -jar testAutoConfig-0.0.1-SNAPSHOT.jar

会打印出antx.properties文件中的值,而不是项目原来的test.properties中定义的值


修改antx.properties文件中的值,再次执行:

mvn clean install
cd targat
<pre name="code" class="plain">java -jar testAutoConfig-0.0.1-SNAPSHOT.jar

 又会打印出新修改的值 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ament_cmake_autoConfig.cmake和ament_cmake_auto-config.cmake都是用于自动配置ament_cmake包的CMake模块文件。这些文件包含了一些CMake变量和函数,用于设置ament_cmake包的构建和安装过程中所需的依赖项、编译选项和安装目录等信息。这些模块文件通常由ament_cmake包提供,可以在CMakeLists.txt文件中使用include()命令来加载它们,以便使用其中定义的功能和变量。具体来说,ament_cmake_autoConfig.cmake用于配置ament_cmake包的构建,而ament_cmake_auto-config.cmake用于配置ament_cmake包的安装。这两个文件都是通过在CMake的扩展点系统中使用的,以实现代码的模块化和灵活性。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【ROS2原理3】:构建系统“ament_cmake”和构建工具“ament_tools”](https://blog.csdn.net/gongdiwudu/article/details/126192244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ROS2学习(十六).ROS概念 - 构建系统](https://blog.csdn.net/amuro_ray027/article/details/120201983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值