How to stop intellij-idea from marking profiles/dev as resource root? - spring

My project structure is like this:
src
main
java
profiles
dev
prod
resources
the resources folder is my resource root, but every time I restart IDEA, the profiles/dev folder is ALSO marked as resource root. And when I debug the application, property files inside profile/dev is used instead of files inside the resources folder.
How do I stop this?

The root cause for this may be that your project is a Maven (or Gradle) project and the Maven (or Gradle) project configuration instructs IntelliJ to treat the profiles folder as a resources folder.
In a Maven pom.xml this might look like:
<build>
...
<resources>
<resource>
/path/to/profiles
<resource>
</resource>
</build>
If that's the root cause then your could change your pom.xml to prevent IntelliJ from treating the profiles folder as a resource.
Alternatively, you could explicitly mark this folder as excluded in the Project Structure dialog (though if the root cause is Maven or Gradle build configuration then this would be overriden the next time you reimport the project into IntelliJ) ...
Open File > Project Structure
Select Modules then select whichever module you want to configure
Select the profiles folder
Click on the Excluded button
From the docs:
Excluded roots
Files in excluded folders are ignored by code completion, navigation and inspection. That is why, when you exclude a folder that you don't need at the moment, you can increase the IDE performance.
Normally, compilation output folders are marked as excluded.
Apart from excluding the entire folders, you can also exclude specific files.
Here's a screenshot:

Related

Execute Maven plugin with custom classpath entry

I would like to call a maven plugin with a custom entry on its classpath. This is usually possible by adding <dependencies> inside the <plugin> tag. The problem is, what I would like to add to the classpath is not a maven artifact, but some random folder in my project (the reasons for this are quite obscure, I need a resource file to be present of the classpath, but I must not copy it to the /target/classes due to IDE shenanigans).
Is there any way to specify truly arbitrary classpath entries for a maven plugin?

Maven: How to confine the application output to the target directory

I have a java application that uses maven for build management.
When I run the generated application jar with
java -jar myjar
the output files generated by the application end up in the projects root directory. So if I execute the jar in /my/project/dir and create a filewriter to write to logs/mylog
The resulting file ends up in
/my/project/dir/logs/mylog
Exactly as expected.
HOWEVER:
When maven surefire plugin executes the unit tests, the files end up in the module directory.
Say that i compile a maven project in /my/module.
The compiled files end up in /my/module/target/classes.
When maven executes these classes, through unit tests, the output of the same classes ends up in
/my/module/logs/mylog
I would like the files to end up in the target dir like
/my/module/target/logs/mylog
As this is where the class files reside.
So I am looking for a way to configure maven surefire to define the java classes' root directory to point to target instead of the module dir.
EDIT:
I have found this post:
Maven: change the directory in which tests are executed
That seems to attempt a fix to my problem. However, if i set the workingdirectory to my target dir, the tests can no longer find my resources, even if they are copied from the modules ${basedir} to ${basedir}/target
You should set your application working dir to ./target (or in a Maven property way: ${project.build.directory})
Solution:
By default, the maven surefire plugin executes its tests in the modules main directory.
In order to avoid this, set the workingdirectory of the plugin to the target directory.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>...</version>
<configuration>
<workingDirectory>${project.build.directory}</workingDirectory>
<basedir>${project.build.directory}</basedir>
</configuration>
</plugin>
Note that in case of tests that rely on some resources with root in the modules basedir, you need to copy these over. The easiest way i can find is with the resource plugin.
<build>
<resources>
<resource>
<directory>dependency</directory>
<targetPath>${project.build.directory}/dependency</targetPath>
</resource>
...
</build>
I can't tell you exactly how to do what you need to do because there is no real information
I can give you a hint about what to look at using.
Maven profiles can change the source, and behavior of Maven when they are selectively enabled.
Profiles are specifically designed to do just what you want to do.

Intellij: jboss-ejb3.xml entry keeps disappearing from ${PROJECT_DIR}/.idea/artifacts/XXX_war_exploded.xml

Why is it that although I've
set the proper path to the JBoss EJB deployment descriptor in my project's EJB facet
added jboss-ejb3.xml to Intellij's artifact Patrac-web:war exploded's <output root>/WEB-INF
that any time I make the simplest change to pom.xml Intellij removes the following entry from ${PROJECT_DIR}/.idea/artifacts/Patrac_web_war_exploded.xml:
<element id="file-copy" path="$PROJECT_DIR$/Patrac-ejb/src/main/resources/META-INF/jboss-ejb3.xml" />
and, as a result, jboss-ejb3.xml does not get copied to the target directory?
It's as though each time I make a change to pom.xml Intellij "reloads" the deployment configuration using the POM to override what settings I make within the IDE. Perhaps because I have no entry in my pom.xml for copying jboss-ejb3.xml from source directory to target directory the settings I make in Intellij IDE keep disappearing whenever Intellij "reloads." Pure conjecture on my part, but this is what seems to be happening.
If so, what change do I need to make to pom.xml in order to make this stop happening?
When a project is (re)imported from Maven IDEA configures it such way that when you invoke 'Build' from IDEA it produces the same result as Maven's 'package' goal. If you need to copy jboss-ejb3.xml to WEB-INF just put it under 'src/main/webapp/WEB-INF' directory and it will be copied by Maven and so do IDEA.
Here is an alternative to Nik's solution that I tried because I wanted to leave jboss-ejb3.xml in META-INF. Taking a look at the maven documentation, which shows how to treat jboss-ejb3.xml as a web resource and copy it into WEB-INF, I added the following to the maven-war-plugin configuration and the problem was resolved. Well, kinda sorta. But not really.
<webResources>
<resource>
<directory>../Patrac-ejb/src/main/resources/META-INF</directory>
<includes>
<include>jboss-ejb3.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
Although doing it this way eliminated the need to fiddle around with IDEA facet configuration settings (because Maven was configured to copy the file, not IDEA), a new problem was introduced: two copies of jboss-ejb3.xml appeared in the WAR, one in WEB-INF and the other inside the EJB JAR (in META-INF). Although there were no apparent consequences (the application ran just fine) I preferred not to have a duplicate copy of the descriptor located inside the EJB JAR.
Using the Maven EJB plugin documentation, I tried to add an exclusion to my maven-ejb-plugin configuration e.g.
<configuration>
<ejbVersion>3.1</ejbVersion>
<excludes>
<exclude>**/jboss-ejb3.xml</exclude>
</excludes>
</configuration>
This should have prevented the duplicate copy of jboss-ejb3.xml from appearing in the EJB JAR META-INF but it didn't work for me, and after fruitlessly googling various variations of "maven-ejb-plugin excludes not working properly" I gave up.
If I could have gotten the excludes to work then I would have preferred this solution over moving jboss-ejb3.xml into src/main/webapp/WEB-INF because although this solution is slightly more complex (it requires additional Maven configuration settings in two POMs), the EJB-related descriptor would remain in the EJB module.
I've decided to go with Nik's solution until I can resolve the excludes problem.

Maven resource folder isn't created

I'm using Eclipse Indigo with m2e plugin, and I've added to the build section of my pom.xml a resources tag. However the resource directory doesn't get created. I've also called Maven -> Update Project Configuration...
<build>
<finalName>...</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
What am I missing?
If the resources folder itself doesn't exist create the folder under the folder main. Then Right click the project, go to Maven -> Update Project. It worked for me.
It appears that you may not have created the maven project correctly. Perhaps you may have chosen an archetype, which does not do this correctly.
One possible way (which worked for me) is as follows:
When I do Create a new maven project, and choose Create a simple project (skip archetype selection, I do get the default folders created (src/main/java, src/main/resources, src/test/java, src/test/resources, along with the pom.xml).
As an alternative to creating a new project you could also have fixed the existing one by doing the following:
Do a non-destructive delete of the project from Eclipse i.e. do NOT check "Delete project contents on disk"
In the file system, remove the .settings/, .classpath, and .project files.
Also in the file system, add the resources folders under src/main and src/test.
In Eclipse, do an "Existing Maven Projects" import with the project's folder as the root.
This tag <resource>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
is not needed as this point because is the default resource folder maven looks for. Just need to add this
<resources>
<resource>
<directory>src/newDir/dir</directory>
</resource>
</resources>
at your pom.xml if you gonna use a different folder as a resource for your project.
You can create the folders in your file system and after that, you can change the .classpath file in the root of the project, next to the pom.xml
Just add something like:
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
After create the folders and update the .classpath you only have to refresh (F5) in Eclipse.
PS: I don't know if this depends of your maven version, check if it is similar to the other "classpathentry" that you have.
If you are getting only one folder inside Java Resources(ie,src/main/java),and if you are looking for other folders(src/main/resoures, src/test/java) then do the following steps:[Dont forget to configure Server(like Tomcate)]
1)Right click on your project
2)Build path
3)congigure build path
4)Libraries
5)Add library
6)Server Runtime
7)then Select your server(In my case i have selected Tomcat)
8)Finish
Every Maven Archetype has a different directory structure. See here the Maven website on the different available archetypes: https://maven.apache.org/archetypes/index.html
When choosing an Maven Archetype, you can follow the link above and click on the various archetypes and it will show you the directory structure that comes with each archetype. For example, the Maven Simple Project Archetype looks like this:
Maven Simple Project Archetype directory structure
This worked for me. ->
In Java Build Path tick both JRE System library and Maven Dependencies and click on Apply and close.
My Maven project was created without src/main/resource and src/test/resource. I tried the below steps :
Create New Maven Project-->On "New Maven Project" screen -->Tick the "Create a simple project(skip archetype selection)"-->Provide the Project name-->Click Finish
Now you can see the resuorce folders created.
if you're using intellij as your IDE, you can simply create the resources directory by right clicking the main folder in your project and attempt to make a new Directory, if you already don't have the resources Directory for your maven project, intellij has a suggestion for you to create it.
I cleaned my project and solved my problem this way.
Menu > Project > Clean...

maven-jetty plugin not using correct paths

I am using the jetty-maven plugin to deploy a web-app for integration testing. I have a project which consists of a two POMs. One in the root, and one in a directory in the project root (call this the webapp directory). When I run the integration test from maven (which uses the jetty-maven plugin), in the webapp directory, it works perfectly fine. However, when I run it from the root directory, it goes inside the webapp directory and runs the test but for some reason it gets the paths wrong. (As a result, all resources cannot be found).
It concatenates the root directory path to the webapp resources path. It also does not understand the dot operator in a path.
For example: if it was to look for "./resources/resource.xml"
It would work fine if the test was run from the webapp directory, however if run from the root directory it will make the path: "c:/project/./resources/resource.xml". It is the jetty-maven plugin that is doing this but I am not sure why.
Is there a way to fix this?
Here is relevant part of my webapp directory POM
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webApp>${basedir}/target/webappSNAPSHOT.war</webApp>
<extraClasspath>${project.build.testOutputDirectory}</extraClasspath>
<testClassesDirectory>${basedir}/target/test-classes</testClassesDirectory>
<useTestClasspath>true</useTestClasspath>
<stopPort>9929</stopPort>
<stopKey>stopKey</stopKey>
</configuration>
Most likely you just need to specify ${basedir} in your web app resources path.

Resources