0

Need to be pointed in the right direction on this perhaps, but if I add a "provided" dependency that is not included in the tomcat set of provided dependencies, running tomcat7:run from within eclipse fails with a classnotfoundexception on the class from the provided scope jar.

It needs to "provided" because it's a custom jar from a separate project that I've run mvn install on and for production am copying the jar to the $CATALINA_BASE/shared directory so that it's available (as a singleton) across applications/webapps.

    <dependency>
        <groupId>IndexFileAccessTracker</groupId>
        <artifactId>IndexFileAccessTracker</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>

Only way I see (with my limited knowledge of Maven and the Tomcat7 plugin) is to change the scope to compile when running tomcat from the plugin in Eclipse and then change the scope back to provided when running the package goal.

Are there solutions to this? I tried adding the dependency to the the tomcat maven plugin (keeping the main maven dependency as provided but got the same class not found error:

            <!-- For Maven Tomcat Plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/CounterWebApp</path>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>IndexFileAccessTracker</groupId>
                    <artifactId>IndexFileAccessTracker</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </plugin>

Again, it needs to be provided in the main Maven dependency because I don't want it included in the deployed WAR.

Hendrik Jander
  • 5,515
  • 3
  • 23
  • 31

2 Answers2

1

Resolved by using profiles, similar to https://stackoverflow.com/a/5951630

...
</dependencies>
<profiles>
<profile>
    <id>runineclipse</id>
    <dependencies>
        <dependency>
            <groupId>IndexFileAccessTracker</groupId>
            <artifactId>IndexFileAccessTracker</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</profile>  
</profiles>
<build>
...

Then in my run/debug configuration just added runineclipse to the Profiles: box. (On a side note, to do step through debugging I had to manually add the project to the Source tab.)

The build configuration was just the same package in the Goals: box; and I left the original dependency to have scope provided.

Community
  • 1
  • 1
0

The tomcat7-maven-plugin and its run goal

Requires dependency resolution of artifacts in scope: test

Everythig that is on the compile classpath is also on the test classpath. Thats why it is working with scope compile.

So the solution in your case would be to mark your dependency as test what even is (imo) semantically correct.

This will make the library available at local test-time, but not in the final artifact.

Hendrik Jander
  • 5,515
  • 3
  • 23
  • 31
  • seems to only work in compile. I tried changing to test and still get java.lang.ClassNotFoundException: com.awgtek.indextracker.HtmlPageFilter at org.apache.catalina.loader.WebappClassLoader.loadClass when doing tomcat:run –  Jan 19 '16 at 12:35
  • I checked the source code of the tomcat plugin and found a flag that [is named *useTestClasspath*](https://github.com/apache/tomcat-maven-plugin/blob/trunk/tomcat7-maven-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Ftomcat%2Fmaven%2Fplugin%2Ftomcat7%2Frun%2FRunMojo.java#L109) . Thats a bit weird as seems to contradict the [plugins header definition to use always the test-classpath](https://github.com/apache/tomcat-maven-plugin/blob/trunk/tomcat7-maven-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Ftomcat%2Fmaven%2Fplugin%2Ftomcat7%2Frun%2FRunMojo.java#L58). You can try with useTestClasspath = true. – Hendrik Jander Jan 19 '16 at 15:28
  • Tried adding true to plugin configuration section, with scope set to 'test' for IndexFileAccessTracker. Still get ClassNotFoundException. –  Jan 20 '16 at 20:50