0

I want to manually add classes to my maven project after it has been compiled, because I want to use them for the tests.

After reading this Maven Classpath, I thought it would be enough to add my class files to the target/classes folder, but it didn't work. I ran the tests with the -verbose:class flag and the class hadn't been loaded.

The name of my class is foo.Foo , so I put it in target/classes/foo/Foo.class. This shoud be right.

What am I doing wrong? I try to avoid manipulating the pom.

skappler
  • 722
  • 1
  • 10
  • 26

2 Answers2

0

If you want some classes to be in your classpath during your test phase, they should be located under the src/test/java folder. Everything below the target folder is for what maven produces and you rarely have to put files in there by yourself.

gizmo
  • 11,819
  • 6
  • 44
  • 61
  • I only want to run the tests without compiling them. They have been compiled in before. I use `mvn surefire:test` to execute the tests. If my class was in the src folder it would need to be compiled – skappler Nov 18 '14 at 12:54
0

Seems like you are asking about how to add test-scope dependencies in a maven project. You can make a jar file of your classes, and install it locally in your repository. Use the following links for instructions:
Maven - install jar locally
How to add local jar files in maven project?

Add group and artifact id of your preference.

Then you have to add this as a test scope dependency.

  <dependency>
    <groupId>your-group-id</groupId>
    <artifactId>your-artifact-id</artifactId>
    <version>1.0</version>
    <type>jar</type>
    <scope>test</scope>
  </dependency>

The dependency will be used only for testing. It won't end up into your deployable.

Don't be afraid to modify the pom. That's how maven projects are supposed to be managed.

Community
  • 1
  • 1
gregdim
  • 2,031
  • 13
  • 15
  • The problem why I don't want to modify the pom is not the deployability. I try to add an analysis for the test runs and want to run this automatically on several maven projects and automatically manipulating the pom could get pretty ugly. – skappler Nov 18 '14 at 13:45
  • But isn't it also inconvenient to manually adding classes to each project? Left aside that whatever you put under target will be deleted with each maven clean so you have to write somewhere down "I will never execute clean for this project" – gregdim Nov 18 '14 at 14:02
  • No I only want to run this once per project. So a simple shell script could do this for me. Build the project, copy the needed classes to the target folder, execute mvn surefire:test and store the results. – skappler Nov 18 '14 at 14:10