maven test cannot find resources - testing

I am trying to do maven test.
I have set resource directory
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
I put a folder called inbound inside this directory.
In my junit, I am trying to open it as new File("Inbound"), but it always cannot find it.
I have have a inbound copy under my project root. The junit code cannot find it.
Could any one please give me some suggestion?
Thank you

If you use src/test/resources, then you don't have to specify it in your pom.xml as it is the default resources path for tests.
Regarding your problem, you should not use new File("myfile"); but MyTestClass.class.getResourceAsStream("/myfile"); instead.

1) Add it into your src/test/resources
2) when load use classpath:YOUR_FILE
PS: you do not need to specify that code in your pom.xml, those folders are maven defaults.

Related

IntelliJ: how to make non java files copied to the bin directory as well?

My module contains some non java files along the java source files. When the module is built, the java files are copied to the bin folder (and included in the jar artifact), but the non java files are left out.
I need them to be copied as well (this is what Eclipse does). Note, that they do appear in the project tree view on the left, I did not exclude them in any way.
How can I make them get into the bin folder (jar artifact)?
Thanks.
Settings (Preferences on Mac) | Compiler | Resource Patterns.
This question duplicates/relates to:
copy jbehave stories into target directory using IntelliJ Idea
IntelliJ, Akka and Configuration files
IntelliJ IDEA 11.1.2 does not copy SQL files to the out folder
Add a properties file to IntelliJ's classpath
import images into an intelliJ Java project
Intellij - how do I add a text file to the resources
Null Pointer Exception for read properties file in Idea
IntelliJ Idea - resource SQL files not being copied to target
Scala getClass.getResource() returning null
On IDEA 14.1.4, the xml file in src/main/java/my/package folder is not copied. My compiler settings are !?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj.
I changed the gradle file by adding:
test {
resources {
srcDir 'src/main/java'
include '**/*.xml'
}
}
It starts working. I am not sure if I have missed anything, but I could not find that part reflected on project settings.
If you are working with Maven, the following code should have the same effect:
<build>
<testResources>
<testResource>
<filtering>false</filtering>
<directory>src/test/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>
I posted it here as an answer, because it may help someone who has the same issue and the above answers may not work.
Uncheck use external build in project compiler setting.
Using CrazyCoder's info about version 12 (which I'm not using), I added the following as my resource pattern which worked well:
*.*;!*.form;!*.java;!*.class;!*.groovy;!*.as;!*.flex;!*.kt

Maven2 unpack include only internal files

I have the next artifact item for maven unpack
<artifactItem>
<groupId>blabla</groupId>
<artifactId>foo-bar</artifactId>
<outputDirectory>${project.build.directory}/outer-resources/META-INF/wsdl/</outputDirectory>
<includes>xsd/*</includes>
</artifactItem>
I need to copy ONLY files and subfolders from xsd, but not xsd folder. How can I use includes/excludes to make this?
It looks like you may not be able to do this by using maven dependency plugin alone.
But you can try using a combination of maven dependency plugin and maven resource plugin to achieve this. You can use maven dependency plugin to unpack the contents of the dependency to a specific directory and then use maven resource plugin to copy the desired contents from there to another location, excluding the parent xsd folder.
You would need to ensure that both the plugins are invoked in the same phase and goal in the correct sequence.

Maven variable for reactor root

In a multi-module maven project, is there a variable that points to the root project folder?
${project.basedir} points to the current project's directory,
${project.parent.basedir} points to the parent project's directory,
but is there a variable that always points to the root directory (the one from which the maven command was executed), no matter from which project inside the reactor?
I realized that the problem I wanted to solve is pretty much unsolvable. I wanted a variable that pointed to either project.basedir, project.parent.basedir, project.parent.parent.basedir etc, whichever is higher. But since a project's parent pom need not be it's parent in the file system, my whole approach won't help. So I am accepting Pascal's answer because it answers my question (even if my question does not solve my problem).
is there a variable that always points to the root directory (the one from which the maven command was executed)
user.dir (the working directory) should be that directory.
In the latest maven, you can use ${maven.multiModuleProjectDirectory}.
In Maven 3, ${session.executionRootDirectory} is "a variable that always points to the ... directory ... from which the maven command was executed."
Note that this is distinct from a property that gives the top-level root directory of a multi-module project, regardless of where in the directory structure mvn is executed from. Such a property does not exist to my knowledge, but you can use the ${basedir}/.. hack to achieve it. See this thread on maven-users for more details.
See also: Finding the root directory of a multi module maven reactor project
Use directory-maven-plugin with directory-of goal.
Unlike other suggestions:
This solution works for multi-module projects.
It works whether you build the whole project or a sub-module
It works whether you run maven from the root folder or a sub-module (unlike ${session.executionRootDirectory}
There's no need to set a relative path property in each and every sub-module!
The plugin lets you set a property of your choice to the absolute-path of any of the project's modules. In my case I set it to the root module...
In my project root pom:
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>directory-of</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>myproject.basedir</property>
<project>
<groupId>com.my.domain</groupId>
<artifactId>my-root-artifact</artifactId>
</project>
</configuration>
</execution>
</executions>
</plugin>
From then on, ${myproject.basedir} in any sub-module pom always has the path of the project root module. And of course, you can set the property to any module, not just the root...
Such property can be created using: directory-maven-plugin.
Using the plugin's highest-basedir goal you can assign the root path to any property you specify.
For me, there was a need for root directory during variable interpolation, not for plugins section - for local directory relative to root with hand-crafted jars. I know this is a bad practice to have local directory with jars, but this was a requirement of project.
Why I was unable to use different solutions:
${session.executionRootDirectory} and ${user.dir} are tied with directory from which maven command was executed. I want to refer to the same directory independently of directory, from which maven was launched.
${project.basedir} ,as mentioned above, points to current project directory, so child modules will search for jars in wrong location.
I had about 100 projects, so defining relative paths or usage of accepted answer for this question is quite complex in my case.
Directory plugin can be used only for plugin configurations, not for variable interpolation
So, in my case with bad requirements I have used environment variable which refers project root and used it in pom.xml. Use it as last resort, when other solutions do not work. Here is example, how I use environment variable in my case:
<repositories>
<repository>
<id>local-maven-repo</id>
<!--NOTE: export PROJECT_ROOT=<location>-->
<url>file:///${env.PROJECT_ROOT}/local-repo</url>
</repository>
</repositories>
As far I think, there is no such variable. There are only workaround like in accepted answer of Maven2 property that indicates the parent directory .

Specifying jar file in maven build argument

We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
This is because the driver jar is available in our app server lib folder and we don't want to specify it in the dependencies of our projects.
Couldn't find a suitable solution googling it, hence requesting for an expert solution here.
Any workaround would be of greater help.
Thanks in advance.
The usual way would be to add a dependency to the database driver and limit the dependency to testing (test scope). So the library is available for unit tests but will not deployed and jar'ed.
Practically spoken, I'd create a maven artifact for this driver (just a basic POM file) and place it on the build servers maven repository (or the nexus, if you use it for the projects).
I'm using a dependency with scope set to 'system' to reference a jar that is available in the container but not in any maven repository. In this case the jar is put in a folder named 'lib' in the project like this, :
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/library.jar</systemPath>
</dependency>
The groupId, artifactId and version can be set to any value you want, the trick was that system dependencies have to be given with an absolute path, which is worked around by using the project.basedir property. It should also be possible to specify the complete path as a property.
We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Well, the maven way would be to declare the DB2 driver as dependency with a test scope in a parent project.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
You could use the additionalClasspathElement in the plugin configuration to pass the path to the driver:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
If you variablelize it, you could pass the value on the command line.
But to be honest, I can't understand why you don't install the driver in a corporate repository and declare it as dependency. And if you don't have a corporate repository, use a file based repo as described in this previous answer (please, don't use the system scope bad practice). There is no good reason to go the hacky way.

Adding additional resources to a Maven pom

I have a super pom defined in which I specify a "resources" directory for resources. In one of my projects, my pom extends that super pom, and I would like to add an additional resource. I tried:
<resources>
<resource>
<targetPath>/</targetPath>
<directory>additionalDir</directory>
</resource>
</resources>
But that results in only additionalDir being in the resources and does not include the super pom's resources. Is there a way to extend the super pom's resources?
The behaviour you've described is expected.
Remember that your super POM is inheriting from Maven's default POM and pretty much any plugin that you define in your pom effectively overrides the setting in the default POM. As an example, to enable filtering on the default resource, you have to specify the path in your POM. You do this:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Not this:
<resources>
<resource>
<filtering>true</filtering>
</resource>
</resources>
The default directory does not bubble up for you, even if you've specified it there. There might be a special MOJO to do it for you, but I couldn't find one.
See build-helper-maven-plugin's usage page.
I know this question is pretty old by now but maybe someone will find this usefull:
You can easily do that using the maven resource plugin.
Basically, what it does by default (invoking the resources:resources goal) is moving the content of all directories listed in the build/resources section using the declared filter into the ${project.build.outputDirectory}
If you now want to add some additional resources without influencing these sections, you can simply move the resources there yourself. An no, this does not mean drag and dropping it there.
The Maven Resources Pluginprovides a goal named resources:copy-resources that does exactly that for you: Coping files using filters from one place to another, or as the plugin doc states:
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element and attach it to a phase
The obvious advantage compared to the build-helper, that is suggested in other solutions, is that, while the build-helper is only able to take action during the generate-sources phase, the resources plugin works in any phase. (Though it would be wise to execute it before the packaging stage)
A full example on how to use this is provided here, as part of the plugins project page.
What this does is coping the content of the not listed resource directory "src/non-packaged-resources"
into "${basedir}/target/extra-resources". But since the jar plugin (that creates the jar archive) basically creates a zip archive of the directory "${project.build.outputDirectory}", you may want to put it there.
Yes, maven overrides the parent POM's resources when redefining new resource section.
However, a simple way to add resources is to
Remove the resources section from your POM
Show The effective POM
Copy the resources from your effective POM into your project
Replace absolute paths with relative ones
Add your additional resources