我可以用maven插件更新文件内容吗?

ngynwnxp  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(509)

我想根据属性文件的值更新context.xml和web.xml文件的内容。我想设置可由我的web应用使用的数据库连接。有没有可能以动态的方式添加这些内容?
我目前正在使用pom中的以下构建部分进行一些更新

  1. <build>
  2. <finalName>ROOT</finalName>
  3. <filters>
  4. <filter>src/main/resources/environmentProperties/env1.properties</filter>
  5. <filter>src/main/resources/environmentProperties/env2.properties</filter>
  6. <filter>src/main/resources/environmentProperties/env3.properties</filter>
  7. <filter>src/main/resources/environmentProperties/env4.properties</filter>
  8. </filters>
  9. <plugins>
  10. <plugin>
  11. <groupId>org.apache.maven.plugins</groupId>
  12. <artifactId>maven-war-plugin</artifactId>
  13. <version>${maven-war-plugin-version}</version>
  14. <configuration>
  15. <webResources>
  16. <resource>
  17. <directory>${basedir}\src\main\resources\META-INF</directory>
  18. <filtering>true</filtering>
  19. <targetPath>META-INF</targetPath>
  20. <includes>
  21. <include>**\context.xml</include>
  22. </includes>
  23. </resource>
  24. </webResources>
  25. <warSourceDirectory>src/main/webapp</warSourceDirectory>
  26. <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
  27. </configuration>
  28. </plugin>
  29. </plugins>
  30. </build>

在我的web.xml文件中,我有以下内容

  1. <resource-ref>
  2. <description>Env1 Database Connection</description>
  3. <res-ref-name>jdbc/Env1</res-ref-name>
  4. <res-type>javax.sql.DataSource</res-type>
  5. <res-auth>Container</res-auth>
  6. </resource-ref>
  7. <resource-ref>
  8. <description>Env2 Database Connection</description>
  9. <res-ref-name>jdbc/Env2</res-ref-name>
  10. <res-type>javax.sql.DataSource</res-type>
  11. <res-auth>Container</res-auth>
  12. </resource-ref>
  13. <resource-ref>
  14. <description>Env3 Database Connection</description>
  15. <res-ref-name>jdbc/Env3</res-ref-name>
  16. <res-type>javax.sql.DataSource</res-type>
  17. <res-auth>Container</res-auth>
  18. </resource-ref>
  19. <resource-ref>
  20. <description>Env4 Database Connection</description>
  21. <res-ref-name>jdbc/Env4</res-ref-name>
  22. <res-type>javax.sql.DataSource</res-type>
  23. <res-auth>Container</res-auth>
  24. </resource-ref>

我真的希望能够在environmentproperties文件夹中拥有所需数量的属性文件,然后更新web.xml(和context.xml)文件,为每个环境添加适当的条目。这可能吗。
我见过apachevelocity做循环构造,但不知道是否有maven插件允许我使用它。
谢谢,保罗

9gm1akwq

9gm1akwq1#

我很幸运使用了 stringtemplate http://www.stringtemplate.org/ 获取模板并渲染它。我使用它来创建基于当前maven版本的docker文件,这样就不必每次发布新jar版本时都更新它们。

  1. <plugin>
  2. <groupId>com.webguys</groupId>
  3. <artifactId>string-template-maven-plugin</artifactId>
  4. <version>1.1</version>
  5. <configuration>
  6. <templates>
  7. <template>
  8. <directory>${basedir}/src/main/resources/templates</directory>
  9. <name>dockerfile</name>
  10. <target>
  11. ${basedir}/target/Dockerfile
  12. </target>
  13. <properties>
  14. <jar>${project.build.finalName}</jar>
  15. </properties>
  16. </template>
  17. </templates>
  18. </configuration>
  19. <executions>
  20. <execution>
  21. <phase>generate-sources</phase>
  22. <goals>
  23. <goal>render</goal>
  24. </goals>
  25. </execution>
  26. </executions>
  27. </plugin>

此插件配置获取此dockerfile:

  1. dockerfile(jar) ::= <<
  2. FROM anapsix/alpine-java
  3. MAINTAINER saisols.com
  4. RUN mkdir /opt/app
  5. ADD . /opt/app
  6. WORKDIR /opt/app
  7. CMD ["java", "-jar", "<jar>.jar"]
  8. >>

它呈现 CMD 使用要执行的jar的正确名称。
这并不是你想要的,但我用这个策略来做你想做的事。
您可以使用 properties-maven-plugin 在http://www.mojohaus.org/properties-maven-plugin/usage.html

  1. <plugin>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>properties-maven-plugin</artifactId>
  4. <version>1.0.0</version>
  5. <executions>
  6. <execution>
  7. <phase>initialize</phase>
  8. <goals>
  9. <goal>read-project-properties</goal>
  10. </goals>
  11. <configuration>
  12. <files>
  13. <file>etc/config/dev.properties</file>
  14. </files>
  15. </configuration>
  16. </execution>
  17. </executions>
  18. </plugin>
展开查看全部

相关问题