【问题标题】:How to filter applicationContext using maven war plugin如何使用 Maven 战争插件过滤 applicationContext
【发布时间】:2016-08-05 09:54:16
【问题描述】:

我发现了许多类似的相关问题,但我仍然无法使其正常工作。 这是我的网络应用程序的 pom.xml

<profiles>
    <profile>
      <id>mysql</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <dialect>org.hibernate.dialect.MySQLDialect</dialect>
        <driver>com.mysql.jdbc.Driver</driver>
        <url>jdbc:mysql://localhost/exercisedb</url>
        <username>root</username>
        <password>password</password>
      </properties>
    </profile>
  </profiles>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <webResources>
                <resource>
                    <filtering>true</filtering>
                    <directory>src/main/webapp/WEB-INF</directory>
                    <targetPath>WEB-INF</targetPath>
                </resource>
            </webResources>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

这是我试图在我的 applicationContext 中过滤的 bean,它位于 src/main/webapp/WEB-INF/spring/appServlet 文件夹中

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

我运行应用程序并收到此错误

Cannot load JDBC driver class '${driver}'
java.lang.ClassNotFoundException: ${driver}

我假设属性没有在构建时过滤。

编辑:webapp pom.xml

<webResources>
 <resource>
  <filtering>true</filtering>
  <directory>src/main/webapp/WEB-INF/spring/appServlet</directory>
  <targetPath>WEB-INF/spring/appServlet</targetPath>
  <includes>
   <include>applicationContext.xml</include>
  </includes>
 </resource>
</webResources>

现在过滤但使用 mvn jetty:run,同样的错误 我尝试部署到 tomcat(不是 maven 中的插件)并且它可以工作。

【问题讨论】:

  • 试试不带引号的${driver}

标签: java spring maven


【解决方案1】:

这是jetty-maven-plugin 问题,而不是maven-war-plugin

https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#jetty-run-goal中所述,

码头:运行

run 目标在不必构建到 WAR 中的 webapp 上运行。相反,Jetty 从其来源部署 webapp。它在 Maven 默认项目位置中查找 webapp 的组成部分,尽管您可以在插件配置中覆盖这些部分。例如,默认情况下它会查找:

${project.basedir}/src/main/webapp 中的资源

jetty-maven-plugin looks 在带有占位符 ${driver} 的源文件夹上,而不是带有值 com.mysql.jdbc.Driver 的目标文件夹

您可以将 Spring config XML 从 webResources 移动到资源并通过类路径访问它

【讨论】:

  • 如何通过类路径访问它?我在 web.xml 中设置 spring config xml 的位置
  • @DanielBristol, stackoverflow.com/a/6451465/2365727 classpath*:applicationContext*.xml。基本上你有两个选择:让你的应用适应 jetty 插件或者让 jetty-plugin 配置适应应用。很难提出具体的解决方案
猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 2014-08-20
  • 2012-04-05
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
相关资源
最近更新 更多