简体   繁体   中英

How to filter applicationContext using maven war plugin

I found many related questions like this but still I cant make it working. This is the pom.xml of my web-app

<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>

And this is the bean Im trying to filter in my applicationContext which is in src/main/webapp/WEB-INF/spring/appServlet folder

<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>

I run the application and I got this error

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

I assume the properties are not filtered in build time.

EDIT : 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>

Now filtered but using mvn jetty:run, same error I tried deploying to tomcat (not plugin in maven) and it works.

This is jetty-maven-plugin issue, not maven-war-plugin .

As described in 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 looks on source folder with placeholder ${driver}, not to target folder with value com.mysql.jdbc.Driver

You can move Spring config XML from webResources to resources and access it through classpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM