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

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.

Related

Can I import resources like FTL files from another Maven project in my Liferay portlet?

We have several Liferay 6.2 portlets in different Maven projects. Some of these are imported as dependencies by others. While this allows me to access the java classes, I can't figure out how to import Freemarker files from one project in another project.
I assume I would need to access the resources from the dependency project, and then tell Freemarker how to find and include them. Assuming that's true, that leaves me with two questions:
How can I access resources, like FTL files and images, from another project that is list as a dependency in the Maven pom file of my Liferay project, especially in the server-side code?
How can I tell Freemarker where to look for the FTL files in the project that is listed as a dependency in the Mavan pom file?
If I'm wrong about what I need to be doing, then what is the correct way to give Freemarker access to the FTL files?
EDIT: I have a way around the first problem (though it's not a great way, I feel there's probably a better solution). I've tried to set up a Freemarker configuration using the path of the external FTL files, but I don't really know what to DO with the configuration; it doesn't look like it's actually being used.
I eventually found this question:
Maven: Extract dependency resources before test
Using the maven-dependency-plugin code in the original question and in the answer, I was able to copy the FTL files from the dependency into the Freemarker folder of the child project (in the compiled target folders of course), allowing them to be imported by Freemarker without any other changes made to the child project (the FTL files had to be moved to the resources directory in the parent project). Here are the relevant sections of the child project pom file:
I included the parent project in the dependencies:
<dependency>
<groupId>ourGroupId</groupId>
<artifactId>parentArtifactId</artifactId>
<version>1.0.0</version>
</dependency>
And then added the maven-dependency-plugin in the plugins section of the build, using the same artifact id I used in the dependency section:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>parentArtifactId</includeArtifactIds>
<includes>freemarker/*/*.ftl</includes>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I chose the phase and goals that seemed to make sense, I'm not sure if they're "optimal" but they seem to work. The FTL files are inside a sub-folder, under the "freemarker" folder, in the resources directory of the parent file. The "includes" parameter is a bit more restrictive that I thought, it seems I need to have the correct folder depth specified in order to import the files correctly, but that's fine.
Just for clarity, I made sure the resulting location of the copied Freemarker files matched the Freemarker that folder had already been specified in the applicationContext.xml file:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
</bean>

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

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:

Maven install goal does not generate pom for modules

I'm running a multi-module maven project and have an unexpected behavior. First time I'm seeing this...
My parent module configures the install plugin, defining its classifier.
<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<generatePom>true</generatePom>
<classifier>${env}</classifier>
</configuration>
</plugin>
<!-- ... -->
<modules>
<module>webapp-formation</module>
<module>db-formation</module>
</modules>
But when I'm running mvn install the .pom files are not generate for my modules. Only my parent is associated with a .pom file in my repositories. Thus trying to browse to my module's artifact on Archiva (after running mvn depoy of course!) it simply fails. I can browse to the parent but not its children.
So... I need to add the undocumented attribute generatePom to my plugin configuration to have the .pom files generated --copied would be a better word actually-- for all my modules. --I said undocumented attribute because this attribute is documented only for the install-file goal which is not the one ran by default. The install goal is not expecting that attribute...
Of course, if I do not configure my install plugin --so not configuring the classifier-- I have no problem and all .pom files are generated properly.
For you guys, is that a normal behavior? Something that you have already seen? Or should I just file a bug?
Thanks,
Olivier.
What you describe as an undocumented attribute is simply wrong, cause the attributes are specific on a goal base which means the given configuration will not change anything, cause the generatePom attribute is only valid for install-file goal. So you can remove it.
In general such configuration does not make sense, cause if you have different environments you should go a different way. Just removed hte configuration with <classifier>${env}</classifier> as well and try to deploy via:
mvn clean deploy

Maven: Force Jersey to use specific artifact version

I have a Maven repository where I load Jena TDB 0.9.3 (which depends on Jena ARQ 2.9.3), Jersey 1.8 and RMOnto 1.0. The point is, as you expected, to do some analysis on semantic datasets.
It looks like RMOnto has ARQ 2.8.7 built in, as in "hardwired". There isn't any explicit dependency in its pom file, yet the jar file contains a ARQ.class. It's very tricky because you won't notice it with Maven Enforcer Plugin and the like.
It looks like this causes Jersey to use RMOnto's ARQ version instead of the one defined in pom.xml. Here is a minimal example. When you run the test (checks whether or not ARQ.VERSION equals 2.9.3), it succeeds. When you build the project and deploy it on a Tomcat 7, you should see 2.8.7 as output.
Is this behaviour expected and why?
How could one force Jersey to use ARQ 2.9.3?
In case it's not possible, could one isolate RMOnto to use 2.8.7 while the rest of the source uses 2.9.3?
Thanks in advance!
You should define the ARQ 2.9.3 first in the dependencies list. By doing that you force your build to use that specific version. The dependency order is relevant when choosing what artifact to use.
Update
OK, I understand what the problem is.
The RMOnto jar is obviously shaded according to the pom: http://semantic.cs.put.poznan.pl/maven/put/semantic/RMOnto/1.0/RMOnto-1.0.pom.
Tomcat 7 loads the jars in WEB-INF/lib in an undefined order. This means that even if you define ARQ 2.9.3 to be first in your dependencies it will not be the case when the application is run in Tomcat. http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
Good thing is that Tomcat always look in WEB-INF/classes before WEB-INF/lib for dependencies.
So what you can do as a work around is to make sure that the ARQ 2.9.3 version is added to the WEB-INF/classes folder. This can be done using the maven-dependency-plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.3</version>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<excludes>**/META-INF/</excludes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Your war as well as your exploded war will now contain all the classes from ARQ 2.9.3 in the WEB-INF/classes folder. They will be loaded before any jar-file that is in WEB-INF/lib folder.
NB: I have not tested this on Tomcat but I cannot see that it would not work.
NB2: This is a hack. Best thing would be to remove the ARQ packages out of the RMOnto jar.
You should file a defect report against RMOnto. Hard-wiring library code into a jar, instead of including it as a dependency you can manage in the POM, is definitely a bad idea that the code maintainer should fix.
If the files have been copied directly to the RMOnto .jar, the behaviour is expected.
In that case, I'd say the best bet is to hardcode it away, aka remove the ARQ files directly from the package. Opening up the RMOnto-1.0.jar package one can see arq files in the arq folder. What you'd need to do is open up the jar file (it's just a .zip), remove the ARQ files from there, store the edited RMOnto package in your version control / repository and refer to the edited package from there. Also, you'd need to add excludes statement to your pom for the old version of ARC and keep the dependency to the new version.
If you feel like it, it would be also good practice to remove the other dependencies that haven't been mentioned in the RMOnto's pom file, then add them in the RMOnto pom file (and rebuild, if you have the source code). This way Maven mechanism would be aware of them. The file seems to contain a lot of dependencies like this, which will cause headaches in the future.

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...

Resources