Resources not copied to output path in IntelliJ 12.1.4 - intellij-idea

I seem to have done something to tell IntelliJ to not copy source resources (e.g. XML and property files) to the compiler output folder.
Resources are not being copied to the Compiler output path. Test resources are copied to the test ouput folder, but no source resources are copied.
Source folder: src
(this is C:\dev\myproject\src and contains XML files)
Test Source folder: tests\integration\src
(this is C:\dev\myproject\tests\integration\src and contains XML files)
Compiler output:
Use module compile output path
Output path: C:\dev\myproject\build\classes
Test output path: C:\dev\myproject\build\test
Settings -> Compiler -> Resource patterns:
?*.properties;?*.xml;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.html;?*.dtd;?*.tld;?*.ftl
This is preventing me from running integration tests which load files from the classpath. (I do not have full control over the structure of this legacy project and most of the other developers use Eclipse.)
Can anyone give me some pointers as to what I need to do in order to have IntelliJ copy the resource to the output folders?

i had same problems with IntelliJ IDEA 13 using Maven.
i solved it by adding this to my build tag in the pom.xml file:
<build>
...
<resources>
<resource>
<directory>src/com</directory>
<targetPath>com</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
...
</build>
change path according to your project. More about the Maven Resources Plugin here.

I have faced the same issue. Idea seems to have a bug in projects with maven dependencies.
The workarounds I could found so far:
Disable the external build.
Generate an ant build script (Idea does this for you) and compile with that script.
Once you have successfully created your artifacts with either way, Idea continues to use them until the next maven dependency change.

When using Gradle, try adding the resources to the sourceSets (use below code when you have the resource files in the same folder as the source files, otherwise adapt the path)
sourceSets {
main {
java {
srcDirs 'src'
}
resources {
srcDirs 'src'
}
}
test {
java {
srcDirs 'test'
}
resources {
srcDirs 'test'
}
}
}
Refer to How do I add resources to sourceSet with gradle?

In the case of jpa when you add a jpa facet, Intellij creates:
META-INF/persistence.xml under src/main/java
But it does not update the build
Added this to pom.xml to fix the problem:
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>

Related

Using resource in kotlin func - does not work with fat jar (one jar)

I have the following piece of code:
fun main(args: Array<String>) {
val urlForCSR: URL = ClassLoader.getSystemClassLoader().getResource("merchant.id")
// also tried ClassLoader.getSystemResource("merchant.id")
...
The following when run from intelliJ works fine and finds the resource. But when run using the bundled jar it gives a NullPointerException.
Path for resource: /src/main/resources/merchant.id
Path for code: /src/main/java/Route.kt
Following is the Maven config snippet:
...
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>RouteKt</mainClass> <!-- Class generated (for above main func - named Route.kt) -->
</manifest>
</archive>
</configuration>
</plugin>
<!-- Includes the runtime dependencies -->
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Is there any other war to get URL for the above resource which would work with one-jar or other way of making a fat jar.
Jar content:
jar tf target/Route-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
merchant.id
RouteKt$main$1.class
RouteKt.class
META-INF/maven/
META-INF/maven/groupId/
META-INF/maven/groupId/TokenGenerator/
META-INF/maven/groupId/TokenGenerator/pom.xml
META-INF/maven/groupId/TokenGenerator/pom.properties
One-jar content:
META-INF/MANIFEST.MF
main/Route-1.0-SNAPSHOT.jar
lib/kotlin-stdlib-0.1-SNAPSHOT.jar
lib/kotlin-runtime-0.1-SNAPSHOT.jar
lib/spark-core-2.3.jar
lib/slf4j-api-1.7.12.jar
lib/slf4j-simple-1.7.12.jar
lib/jetty-server-9.3.2.v20150730.jar
...
OneJar packages all of your code into a JAR file that also contains other JAR files in the lib/ directory or as a reference to them in other dirs outside the JAR (depending on the onejar system you are using). It uses a custom class loader to make this work. Therefore it defeats your use of the system class loader.
One way to defeat this is to use a class from the same JAR as the resource you want to load, and therefore its class loader is probably setup correctly into the JAR or nested JAR or magic location in Narnia of your resource:
val stream = MyClass::class.java.getResourceAsStream("/merchant.id")
Where MyClass is a class from the same JAR as merchant.id, and the path to the resource must be absolute with the leading /
Be sure to get the stream and not the resource URL which might not be usable to you since it isn't an understandable URL to the system. It could produce a URL for a JAR within a JAR which the rest of Java wouldn't understand, for example file:/path/to/jarfile/bot.jar!/config/file.txt (or worse!). It could work, but I'm not sure when it is a JAR within a JAR with a file in it.
A secondary option:
val stream = Thread.currentThread().contextClassLoader.getResourceAsStream("/merchant.id")
Check your resource names:
In your question you say to read resource merchant.id but then show JAR contents having merchant.id.soft.applepaydev-v1.csr ... make sure these names match if you are trying to load this exact resource.
Resource loading in onejar must be customized, which is ugly. In a link to one resource that talks about resource loading with onejar:
There are a number of ways that Java supports finding and opening resources:
URL findResource(String resource): returns a resource, or null if not found.
Enumeration findResources(String resource): returns an enumeration of resource URL's which match the given string
URL.openConnection(): opens a connection to a resource by its URL.
One-Jar supports all of these mechanisms within the context of a One-Jar file, which may contain Jar files that have duplicate resources.
This problem is referenced from these other Stack Overflow posts, therefore this is pretty much a duplicate question:
Classpath resources inside one-jar packaged via sbt
How to get a path to a resource in a Java JAR file
How to get an URL for a resource directory when using OneJar?
Onejar and resource loading
Googling turns up many more resources.

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

How do I install JQuery into my web app using Maven?

I am looking at using Maven 3 for some web development. I know how to use maven to resolve java based jar files. How do I use maven to resolve JavaScript dependencies for example I want to have maven automatically put jquery in my webapp/js folder?
Is it possible to do transative JavaScript dependencies with Maven 3?
You could create your own "war" that has jquery in it on the path you specified.
Afterwards add that war to your real web application. Maven should merge it. I think it was called somehting like "processing overlay".
I'm using the same for some GWT application where the javascript is generated by a maven plugin and it works well.
Perhaps WebJars can help you:
WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files.
Explicitly and easily manage the client-side dependencies in JVM-based web applications
Use JVM-based build tools (e.g. Maven, Gradle, & SBT) to download your client-side dependencies
Know which client-side dependencies you are using
Transitive dependencies magically appear
Caveat: WebJars does not put the jquery files in the webapp/js folder. Please read documentation to see how it is used with some popular frameworks.
Try adding this to your pom.xml file. It worked for me :
<dependency>
<groupId>com.efsavage.jquery</groupId>
<artifactId>jquery-maven</artifactId>
<version>1.7.2</version>
</dependency>
This library gives you a way to drop jQuery into your project as a Maven dependency, which means you don't have to include these third party files in your own source control.
Source : here
May be you should use one of CDN to include external javascript libs to your webapps?
E.g. for jQuery you can simple include it from any of URLs from http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery
Also libs from CDN will reduce your server bandwidth usage.
You can create a new zip artifact in your repo with your version of jquery inside.
Then use the maven war plugin to deploy it to your war.
Take a close look at <targetPath> and <includes> to be sure to deploy what you need where you need it :)
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<overlays>
<overlay>
<groupId>yourgroupid</groupId>
<artifactId>yourJqueryArtifactId</artifactId>
<type>zip</type>
<targetPath/>
<includes>
<include>js/jquery.js</include>
</includes>
</overlay>
</overlays>
</configuration>
</plugin>
the previous config supposes that you have a directory called 'js' in your zip with jquery.js inside. It will be unzipped in your war at js/jquery.js
More info here

Maven - Best way to refer to a directory on the system path

I am trying to build an RPM from my Maven project. I have 5 different modules and each one has its own pom.xml, In the root I have one pom.xml which builds all modules (Typical Maven Setup). When I build an RPM, I want to include a directory that is not part of the maven directories. Its above a directory [from the root folder that contains my maven modules]. What is the best way to include that in my RPM? or rather what is the best way to refer to a directory with out hardcoding the path? I am confused about ${baseDir} and what it refers to?
Thank you.
${project.basedir} refers to the root of the project, ie where the pom.xml is, so you could use that in <systemPath>${project.baseDir}/../../dirYouWant</systemPath>
In general though, Maven best-practices would frown about relying on the relative paths around your projects from being there. Instead, I suggest deploying those files as there own project to your maven repository (as a zip, jar, whatever), and then getting them as part of your rpm build. Depending on what plugin you are using to build your RPM, you can unpack those files automatically.
Try this
<dependency>
...groupid,artifactid etc..
<scope>system</scope>
<systemPath>path/to/your/jar</systemPath>
</dependency>
Did you mean you want to add another project to your maven build being level above?
you can do it like this :
in your parent pom :
<modules>
<module>../projectdirectory</module>
</modules>
in your projectdirectory pom :
<parent>
<groupId>...</groupId>
<artifactId>...parent...</artifactId>
<version>...</version>
<relativePath>../parentProject/pom.xml</relativePath>
</parent>

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