【问题标题】:Launch tomcat from Maven with context.xml and injected password使用 context.xml 和注入的密码从 Maven 启动 tomcat
【发布时间】:2017-12-23 19:26:04
【问题描述】:

我正在尝试设置一个 Maven pom.xml 文件,以便它会

  • 构建我的战争文件
  • 使用具有数据库资源的 context.xml 启动 tomcat
  • 通过 settings.xml 或属性文件在 context.xml 中设置用户名和密码

我需要用户名和密码在项目外部,这样敏感信息就不会存储在版本控制中。

如果我硬编码用户名和密码,Maven tomcat 插件工作正常。我将 context.xml 文件放入 src/test/resources/tomcat/context.xml 并配置插件以从那里提取它:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration> 
        <url>http://localhost:8080/manager/text</url>
        <contextFile>
        ${project.basedir}/src/test/resources/tomcat/context.xml
        </contextFile>
    </configuration>
</plugin>

我见过将用户名和密码放在 .m2/settings.xml 文件中的示例,如下所示:

  <servers>
    <server>
      <id>demo</id>
      <username>myUser</username>
      <password>myPassword</password>
    </server>
  <servers>

但我不知道如何将这些值“注入”到 context.xml 中。我在 context.xml 中的适当位置添加了以下内容:

${servers.server.demo.username}

${servers.server.username}

但它们并没有解决实际值。

这种事情的最佳做法是什么?

【问题讨论】:

    标签: maven tomcat tomcat7 maven-tomcat-plugin


    【解决方案1】:

    您需要在“配置”中添加“服务器”标签并将其 id 指定为值。 在您的情况下,这是一个“演示”。

    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration> 
            <url>http://localhost:8080/manager/text</url>
            <contextFile>
            ${project.basedir}/src/test/resources/tomcat/context.xml
            </contextFile>
            <server>demo</server>
        </configuration>
    </plugin>
    

    【讨论】:

    • 谢谢。但是我在 context.xml 文件中放了什么?我尝试了 ${servers.server.demo.username}、${servers.server.username} 和 ${username},但这些都不起作用。他们没有被替换。
    • 基于“配置”中的“服务器”标签,Tomcat 插件将从源代码树外部的 .m2/settings.xml 中提取适当的密码和用户名。
    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2014-07-11
    • 2012-12-24
    • 2015-09-09
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多