java - 如何使用maven war插件过滤applicationContext

标签 java spring maven

我发现了很多类似的相关问题,但我仍然无法使其工作。 这是我的网络应用程序的 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>

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

<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 中的插件)并且它有效。

最佳答案

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

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

jetty:run

The run goal runs on a webapp that does not have to be built into a WAR. Instead, Jetty deploys the webapp from its sources. It looks for the constituent parts of a webapp in the Maven default project locations, although you can override these in the plugin configuration. For example, by default it looks for:

resources in ${project.basedir}/src/main/webapp

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

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

关于java - 如何使用maven war插件过滤applicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36613029/

相关文章:

java - 使用 Spring 在 Java 中创建运行时对象

spring - 使用 Spring Security ACL 向现有对象身份授予权限

database - 如何打破循环依赖?

java - tomcat7-maven-plugin 无法从 react 器中找到依赖项

java - Git 存储库的内部版本号 Maven 插件

java - 如何使用其他 SpringBoot 项目的注释

java - java EE 中的 Junit 测试 (JPA)

java - 将 Excel 转换为 .csv 文件时,我无法转换日期并将其保存在 csv 文件中?

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'XXX'

java - 带有 asp.net 的 liferay 门户