简单 maven工程 spring注入 占位符 替换打包

时间:2022-12-27 08:56:41

一:安装必要的环境软件,可以参考网上的资料;


源码连接: http://download.csdn.net/detail/yeshenrenjin/5053039

二: 使用maven模版创建工程;

         1:使用框架创建工程;

        简单 maven工程 spring注入 占位符 替换打包

简单 maven工程 spring注入 占位符 替换打包

点击完成;

          2:生成工程如图所示:

                

            3:添加config文件,作为配置文件夹,并将其作为“Use as source folder”(即:选中config文件,点击右键->BuildPath-->Use as source folder )

                如图:

                           简单 maven工程 spring注入 占位符 替换打包

                4: 添加新类文件

                  

public class InfoCollect {
private String name;
private int age;
private String address;
private String passWord;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}

}
                     5:在pom文件中添加spring相关的依赖

        <dependency>		<groupId>org.springframework</groupId>		<artifactId>spring</artifactId>		<version>2.5.6</version>	</dependency>  </dependencies>
                       我们在config目录下生成相关的配置文件:

                 如下:

	<bean id="infoCollect" class="com.netboy.maven.mavenDo.InfoCollect">
<property name="name" value="netboy" />
<property name="age" value="111" />
<property name="address" value="hangzhou " />
<property name="passWord" value="${password}" />
</bean>

三:  运行程序

输出如下:

                     简单 maven工程 spring注入 占位符 替换打包

四: 自动打包并替换掉占位符

          1: 在src/main目录下创建如图所示文件;

              简单 maven工程 spring注入 占位符 替换打包

            daily.properties 内容为: password = 111111

           online.properties内容为: password = 222222

        A、  pom文件如下:(该方法是替换jar包中的配置文件的占位符)

	<profiles>
<profile>
<id>daily</id>
<build>
<filters>
<filter>src/main/assemble/daily.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>env</name>
<value>daily</value>
</property>
</activation>
</profile>
<profile>
<id>online</id>
<build>
<filters>
<filter>src/main/assemble/online.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<activation>
<property>
<name>env</name>
<value>online</value>
</property>
</activation>
</profile>
</profiles>


        B、 使用插件,替换占位符后,并将工程文件打压为zip的pom如下:


  <build>
<filters>
<filter>src/main/assemble/daily.properties</filter>
</filters>

<resources>
<resource>
<directory>config</directory>
<filtering>true</filtering>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<!-- 打包jar时排除掉 src/main/resources 下的xml和 properties -->
<configuration>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</configuration>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>src/main/assemble/zip.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
</execution>
</executions>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<id>daily</id>
<build>
<filters>
<filter>src/main/assemble/daily.properties</filter>
</filters>

<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assemble/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>online</id>
<build>
<filters>
<filter>src/main/assemble/online.properties</filter>
</filters>

<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assemble/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

 根据需要选择A 或者 B 方法

package-daily.bat 为: 

call mvn -U clean package -Dmaven.test.skip=true -Denv=daily   -e
@pause

package-online.bat 为:

call mvn -U clean package -Dmaven.test.skip=true -Denv=online   -e
@pause


五: 整体框架如图所示:

简单 maven工程 spring注入 占位符 替换打包