Use maven-war-plugin to dynamically package Maven projects (transfer)

Original address: http://nileader.blog.51cto.com/1381108/449956

 

Plugin official address:    http://maven.apache.org/plugins/maven-war-plugin/

When publishing a project, you may encounter such a situation. I hope that on the premise of keeping the source code of the project unchanged, I hope to be able to obtain the corresponding running package for different operating environments. (such as war package)

Then using the configuration file can solve this problem. You can put some parameters related to the running environment in the project into the configuration file, one for each environment, so that I only need to specify this packaging before packaging. I need to specify this packaging You need which configuration file you need, so you can make the running package correspond to the environment. But the disadvantage of this approach is that the selection of configuration files requires human intervention - if you encounter such a situation: so-and-so, you Give me the package of the ** environment. At this time, the usual practice is to immediately recall where I am going to change and let the project know what configuration I want to use.

Now there is another way, using the maven-war-plugin plugin can specify which environment package I want to package when executing the packaging command, without paying attention to what configuration file I am using now. Of course, it only applies to Maven projects .

For example: maven package –P youEnvName so you can type a run package for the environment youEnvName.

The first step  is to prepare different operating parameters for different environments: here, create two files in the src/main/resources directory to configure some different parameters of the two environments. Note that the configuration file needs to be a legal properties file: such as:

  1. src/main/resources/IProject-test.properties   
  2. src/main/resources/IProject-real.properties 

 Corresponding to the test and formal environments respectively. Of course, the content inside is a key-value pair with two keys and a different value.

src/main/resources/IProject-test.properties usually looks like this (just a sample)

  1. system.envid=real  
  2. method.version=1.0.0. release   
  3. ………  

Step 2  , create a new directory under src/main, for example: src/main/packageFilter

Then find the one or more configuration files that you want to use these parameters (usually these configuration files are in the WEB-INF directory), and move them to the src/main/packageFilter directory. ( At this time, you may ask: the configuration file has been changed, do you understand the project? Don't worry.) df 

After these configurations are placed in the src/main/packageFilter directory, the parameters configured above should be modified (just a sample)

The following is an example of referencing a configuration file in spring (my note)

  1. <bean id="myService" class="com.nileader.MyService" init-method="init">  
  2.     <property name="version">  
  3.         <value>${ system.envid }</value>  
  4.     </property>  
  5. </bean

See here, the parameter has a placeholder of  ${key} .

Step 3  , add the dependencies of the packaged plugin in the pom file:

  1. <filters>  
  2.     <filter> src/main/resources/IProject-test.properties </filter>  
  3. </filters>  
  4. <plugin>  
  5.     <groupId>org.apache.maven.plugins</groupId>  
  6.     <artifactId>maven-war-plugin</artifactId>  
  7.     <configuration>  
  8.         <webResources>  
  9.             <resource>  
  10.                 <directory>src/main/packageFilter</directory>  
  11.                 <filtering>true</filtering>  
  12.                 <targetPath>WEB-INF</targetPath>  
  13.             </resource>  
  14.         </webResources>  
  15.     </configuration>  
  16. </plugin

这里就可以解决刚才提到的那个问题了,配置文件看上去已经移动了,但是并不需要告诉你的项目,其实在项目加载配置文件的时候,我们已经将一个完整的,不包含任何占位符的配置放在了项目本应该放的位置 ,这里由下面这段配置实现的:

  1. <resource>  
  2.     <directory>src/main/packageFilter</directory>  
  3.     <filtering>true</filtering>  
  4.     <targetPath>WEB-INF</targetPath>  
  5. </resource

这段配置的意思是: 在打包的时候,要将<directory>src/main/packageFilter</directory>中的文件全部搬到

  <targetPath>WEB-INF</targetPath>

目录中去,并且这个过程是覆盖的.   同时<filtering>true</filtering>指定了在这个”搬运”过程中,需要进行过滤.

另外,这里还有个标签: <filters>….</filters> 先不讲

好了,新的问题出来了,什么是过滤.现在进入最后一个步骤. 

第4步 , 还是在pom.xml文件中, 配置如下信息:

  1. <profiles>  
  2.     profile for bulid   
  3.     <profile>  
  4.         <id>test</id>  
  5.         <build>  
  6.             <filters>  
  7.                 <filter> src/main/resources/IProject-test.properties</filter>  
  8.             </filters>  
  9.         </build>  
  10.     </profile>  
  11.     <profile>  
  12.         <id>real</id>  
  13.         <build>  
  14.             <filters>  
  15.                 <filter> src/main/resources/IProject-real.properties</filter>  
  16.             </filters>  
  17.         </build>  
  18.     </profile>  
  19. </profiles>  
 

好了,现在知道了吧, 根据单词filter可以猜到这个配置是干嘛了.他的工作过程就是在打包的时候,

使用命令

mvn package –P real.

那么他就会找到<id>real</id>对应的那个参数信息src/main/resources/IProject- real.properties, 然后用其中的key-value对来过滤<directory>src/main/packageFilter< /directory>中的文件,并将填充完整后的配置文件全部搬运到<targetPath>WEB-INF</targetPath>中去.另外,由于配置<plugin>的时候留下一个标签

  1. <filters>  
  2.     <filter> src/main/resources/IProject-test.properties </filter>  
  3. </filters>  
  4. <plugin>  
  5. ……..  

这个就是所谓的默认参数,也就是当你执行 mvn package,他默认使用这个文件中的参数来过滤,相当于执行 

mvn package –P test. 

如何调试.

     从这里介绍的方法可以看出, 配置文件中的参数是在打包的时候才会被真正填充进去的, 那么对于那种希望在Eclipse中调试的项目怎么办.(例如用Jetty插件来调试Maven项目的时候, 并不会将项目打包后来执行, 而是直接运行src/main/webapp作为项目root路径.) 

    上面已经提到了,maven-war-plugin 在工作过程中是把 <directory>src/main/packageFilter</directory> 中的文件全部搬到<targetPath>WEB-INF</targetPath>下,并且是覆盖.

所以可以这样处理,在开发 调试的时候,可以在配置文件的目录中放上完整的配置文件,里面的填充需要的参数,这位就可以顺利进行调试了, 并且不影响打包. 

注意事项

     现在可以调试了,但是要注意, 在开发调试过程中,如果对这个配置文件有改动,一定要反映到模板文件中去,所以建议在这个需要被填充的配置中添加上一些注释,可以提醒自己:

  1. <!-- 注意,对这个文件的任何修改必须同步到文件:src/main/packageFilter/****.xml,否则白改了. --> 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058623&siteId=291194637