簡單 maven工程 spring注入 佔位符 替換打包
阿新 • • 發佈:2019-01-25
一:安裝必要的環境軟體,可以參考網上的資料;
二: 使用maven模版建立工程;
1:使用框架建立工程;
點選完成;
2:生成工程如圖所示:
3:新增config檔案,作為配置資料夾,並將其作為“Use as source folder”(即:選中config檔案,點選右鍵->BuildPath-->Use as source folder )
如圖:
4: 新增新類檔案
5:在pom檔案中新增spring相關的依賴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; } }
<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>
三: 執行程式
輸出如下:
四: 自動打包並替換掉佔位符
1: 在src/main目錄下建立如圖所示檔案;
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
五: 整體框架如圖所示: