问题
To what does Maven set the classpath for different goals?
My issue: I have a project that I am building in Jenkins. It gets checked out form SVN. As the next thing, a different file is checked out into workspace-root/mydir/my.properties
Then, maven test
is run.
In the test, a class loads the file by searching it on the classpath. Why does that work? (I'm new to Jenkins and maven and am trying to figure out how the job I'm looking at works...)
回答1:
Maven uses the <dependencies/>
defined in your project as puts them on the classpath of the plugins. Furthermore, in the <plugin/>
section of each plugin, you can define additional <dependencies/>
which are only to be used by the plugin (and will therefore not become part of the final artifact in any way, or visible on the mvn dependency:tree
).
In regards to the tests, it's important to know that:
Resources under
src/main/resources
andsrc/test/resources
are respectively copied totarget/classes
andtarget/test-classes
. These two directories are added to your classpath. (The same is valid forsrc/main/java
andsrc/test/java
).Each Maven plugin is executed in it's own classloader.
When executing tests, the
maven-surefire-plugin
will usually fork your tests into a separate classloader.
Check this link.
回答2:
a class loads the file by searching it on the classpath. Why does that work?
Because this is what maven is designed to do. It knows your dependencies, because you set them and makes assumptions about the structure of the modules you are building e.g. src/main/resources will be added and it add all these to the class path.
来源:https://stackoverflow.com/questions/24412796/what-classpath-does-maven-set-for-different-goals-can-you-provide-a-link-to-off