仅作个人学习时,记录自己遇到的问题所用,不喜勿喷。
对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
又会打印出新修改的值