I have a Java project that when I build doing a mvn clean install
command from the command line, it generates an output target folder.
I have a JUnit test class which takes it's test data from this target folder. When running this test class directly (right click Testclass.java > Run As > Junit Test) works fine since the target folder has already been generated.
However to when I do a mvn clean
(which clears out the target folder and my test data as well) then do a mvn clean install
I get a test failure. Because the test is trying to read from target/test-classes/pdf-content and not from target/pdf-content
How can I make my Testclass read test data from target/
and not target/test-classes
?
Edit:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<id>unpack and rename content</id>
<phase>process-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<properties>
</properties>
<source>
import com.cg.fs.common.FileUtil
import com.cg.fs.common.ZipUtil
import com.cg.fs.common.StringUtil
com.services.fs.common.logging.LoggerFactory.setLogger("com.services.fs.common.logging.SystemOutLogger")
com.services.fs.common.logging.FSLogger.setMinimal(true)
buildHelper = new BuildHelper()
output = new File("target")
pdfContent = new File("target/pdf-content")
// unzip tt intermediate thing
List ttZip = FileUtil.discoverFiles(output, true, "production-content-.*\\.zip")
assert ttZip.size() == 1, "Did not find expected number of content zip " + ttZip.size()
println "Unzipping " + ttZip.get(0)
ZipUtil.unzipToDisk(ttZip.get(0))
// move it to pdf content
List content = FileUtil.discoverDirectories(output, "production-content-.*-intermediate", true)
assert tContent.size() == 1, "Did not find expected number of contents " + tContent.size()
println "Moving " + tContent.get(0) + " to " + pdfContent
success = FileUtil.copy(tContent.get(0), pdfContent)
assert success, "Could not copy content"
// move pdf content
List fpsContent = FileUtil.discoverDirectories(output, “ls-production-content.*", true)
assert fpsContent.size() == 1, "Did not find expected number of ls contents " + fpsContent.size()
println "Moving " + fpsContent.get(0) + " to target/pdf-content"
success = FileUtil.copy(fpsContent.get(0), pdfContent)
assert success, "Could not copy pdf content"
// rename to not be unsupported
List unsupportedDirs = FileUtil.discoverDirectories(pdfContent, "_unsupported_.*", true)
println "Renaming _unsupported content: " + unsupportedDirs
for (File file : unsupportedDirs) {
file.renameTo(new File(file.getParentFile(), file.getName().replace("_unsupported_", "")))
}
</source>
</configuration>
You have the question in reverse: you don't want the maven-surefire-plugin
to read from target
instead of target/test-classes
. What you really want to do is to add your files as test resources of the project.
For static resources, this goes under src/test/resources
. The maven-resources-plugin
will copy those resources to target/test-classes
by default.
For generated resources, you can use the build-helper-maven-plugin:add-test-resource
goal to add your generated content as a test resource. If they are generated under target/pdf-content
, you can have:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.build.directory}/pdf-content</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This will add all files under the folder target/pdf-content
as a test resource in the root of the classpath. If they need to be in a specified package, you can add a <targetPath>
to the configuration and they will be available under the specified targetPath
. Note that the plugin is bound to the generate-test-resources
phase so you need to make sure that the generation of those resources happens before (for example, by declaring before in the POM with the phase generate-test-resources
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.