I’m working on a Maven plugin for JCasGen, which takes an XML type system description file and generates some Java classes. The type system description file often points to other type system description files in the same project via the classpath. JCasGen therefore needs a classpath containing all the XML type system description files in order to run. (These XML files must also be placed in the project’s final jar file because Java code may refer to them via the classpath too.)
The XML files are in src/main/resources so that Maven will copy them over to target/classes and they will be included in the project’s jar file. So the natural classpath to give to the plugin would be target/classes. However, if my plugin runs at the intuitive generate-sources phase, then the XML files in src/main/resources will not yet have been copied to target/classes, and JCasGen will fail.
So how do I structure things so that I can give the right classpath to JCasGen?
Here are a couple things I’ve thought of, but I don’t really know whether they make sense:
-
Run the plugin on the
process-resourcesphase, and just usetarget/classesfor the classpath. This is what I’m currently doing and it seems to work. (I was worried about ensuring that my plugin always runs after the standard Maven copying of resources, but that seems to be what happens by default.) The main problem with this approach is thatprocess-resourcesis not the intuitive phase for a plugin that generates sources. -
Build a classpath by concatenating the resource directories and
target/classes:StringBuilder classpath = new StringBuilder(); for (Resource resource : this.project.getBuild().getResources()) { classpath.append(resource.getDirectory()); classpath.append(File.pathSeparatorChar); } classpath.append(this.project.getBuild().getOutputDirectory());I tried something like this and it seemed to work, but I worry that this could fail for complicated resources with includes or excludes.
Just run the plugin on the
process-resourcesphase. This solution is used by other plugins that generate sources and require access to the classpath:Note though that to get the classpath you should use
project.getCompileClasspathElements(), notproject.getBuild().getOutputDirectory(). The former will make sure you get, e.g. thetarget/classesdirectories of projects on which the current project depends.