如何使用Maven War插件过滤applicationContext

丹尼尔·布里斯托尔

我发现了许多与此相关的问题,但仍然无法使它起作用。这是我的网络应用程序的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中所述

码头:运行

运行目标在不必内置到WAR中的Web应用程序上运行。相反,Jetty从源头部署了webapp。尽管您可以在插件配置中覆盖这些内容,但它会在Maven默认项目位置中查找webapp的组成部分。例如,默认情况下,它查找:

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

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

您可以将Spring config XML从webResources移至资源,并通过classpath进行访问

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章