maven-jetty plugin not using correct paths - maven

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.

Related

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:

how to hot deploy jsp file using tomcat7-maven-plugin?

I use tomcat7 with the tomcat-maven plugin. I am able to make it hotswap my jsp but it only work if I modify it directly in the target. How can I make tomcat also look for changes in my sources directory?
pom.xml
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<serverXml>${project.build.directory}/config/tomcat-config/${usingDb}/server.xml</serverXml>
<tomcatUsers>${project.build.directory}/config/tomcat-config/tomcat-users.xml</tomcatUsers>
<configurationDir>${project.build.directory}/config/tomcat-config</configurationDir>
<additionalClassesDirs>
<classesDir>${project.basedir}/src/main/webapp</classesDir>
</additionalClassesDirs>
<contextReloadable>true</contextReloadable>
<port>${tomcat.http.local.port}</port>
<path>/${url.contextPath}</path>
</configuration>
</plugin>
This depends on how you use/start the maven plugin.
Starting it with
mvn tomcat7:run
should do the trick (in comparison to run-war or any other goal). See details at http://tomcat.apache.org/maven-plugin-2.2/tomcat7-maven-plugin/plugin-info.html
This will actually reload the context in your tomcat. I'm not sure actual "Hot replacement" without reloading the context is possible without third party libraries/plugins like jrebel or similar.
You should be able to run the war:exploded maven goal to get your changes copied from your sources directory to the target directory.
Change your workspace in Eclipse to \tomcat\webapps Since it is just for your work, this should work fine. Whatever changes you make in Eclipse is in the same directory tomcat looks for applications to deploy

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.

Jetty not looking in test-classes directory for resources

I am using maven-jetty plugin and when I use jetty to run a webapp, the webapp does not look in the target/test-classes directory at all for a resource. However, it can find resources in the target/classes directory.
Here is the relevant part of my pom.xml
<configuration>
<webApp>target/webapp.war</webApp>
<testClassesDirectory>target/test-classes/</testClassesDirectory>
<useTestClasspath>true</useTestClasspath>
<stopPort>9966</stopPort>
<stopKey>stopKey</stopKey>
</configuration>
How can I make the maven-jetty plugin look in target/test-classes for resources
Because /src/test/java and /src/test/resources folders are only intended for unit testing. They are not added to maven-jetty-plugin CLASSPATH nor they are included in resulting WAR.

Resources