0

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.

Daniel Bristol
  • 87
  • 3
  • 12

1 Answers1

1

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

Community
  • 1
  • 1
michaldo
  • 4,195
  • 1
  • 39
  • 65
  • how can I access it through classpath? I am setting the location of spring config xml in web.xml – Daniel Bristol Apr 14 '16 at 08:38
  • @DanielBristol, http://stackoverflow.com/a/6451465/2365727 classpath*:applicationContext*.xml. Basically you have 2 options: adapt your application to jetty plugin or adapt jetty-plugin configuration to application. It is hard to propose concrete solution – michaldo Apr 14 '16 at 09:00