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