Log4j2 in Eclipse and Maven - need to have different config files - eclipse

I am using Eclipse and m2e on my workstation.
Sometimes I run my unit tests individually with the built-in Eclipse JUnit functionality to display the results in the JUnit view.
At other times before checking in code, I run the whole test suite and integration tests using the Maven test goal.
However when I forget to change my log4j2 configuration to set the minimum log level to DEBUG, then the Maven process kills Eclipse and I need to restart.
I mostly set the log level to TRACE when I am running individual unit tests.
My log4j2 config file is in my src/test/resources directory.
I also have log4j2 set up in-container to read a config file on the file system.
Can I configure my Maven goal in the Eclipse Run dialog to use a different log4j2 file?

Turned out to be quite simple.
Create a new config file src/test/resources/log4j2-maven.xml
Configure the surefire test runner in the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemProperties>
<property>
<name>log4j.configurationFile</name>
<value>log4j2-maven.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

Related

Eclipse deploying unfiltered .WAR-Archive on Tomcat

i have the following problem: I want to use maven profiles and the maven-war-plugin for filtering my webapplication, so that it's just one click to deploy it either on the live- or on the development server.
When i build the .WAR-archive via Eclipse (Maven-Plugin) and then manually deploy it on the tomcat-server via the tomcat-manager-interface, my application works as expected. (all variables are replaced correctly inside the generated .WAR-archive)
But when i want to run the application using Eclipse's "Run on Server", an exception occurs:
[..] nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.web.authentication.logout.LogoutFilter]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: ${security.casEntryPoint.logoutSuccessUrl} isn't a valid redirect URL
This is the configuration of the maven profiles in my pom.xml:
<profiles>
<profile>
<id>prod</id>
<build>
<filters>
<filter>src/main/filters/dfnaaiAgreement-prod.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>devLocal</id>
<build>
<filters>
<filter>src/main/filters/dfnaaiAgreement-devLocal.properties</filter>
</filters>
</build>
</profile>
This is an example entry for the *.properties file:
security.casEntryPoint.logoutSuccessUrl = https://example_entry/please_logmeout
This is one entry in the settings.xml that should be replaced by the value in the *.properties file:
<security:logout logout-url="/logout" logout-success-url="${security.casEntryPoint.logoutSuccessUrl}" />
and this is the configuration for the maven plugin (in the pom.xml), that is used for filtering the configuration files:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warName>dfnaaiAgreement</warName>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</webResources>
</configuration>
This is my maven build configuration:
Maven-Build configuration (Sorry, not enough reputation to post images directly)
It seems like eclipse's "run on server" deploys the unfiltered application on the tomcat-server.
What am i doing wrong?
Update:
I followed this Link Run Maven webapp on server from Eclipse with properties injection from profile, but i'am still receiving the same Exception.
Here's the adjustment of my project's configuration: Active Maven Profiles
Update: Eclipse (Luna) deploys the webapplication to workspace/.metadata/.plugins/.org.eclipse.wst.server.core/tmp0/wtpwebapps/applicationName. The settings.xml that's deployed there still contains the unfiltered variables, whereas the generated .WAR-Archive (that is located at workspace/applicationName/target) has the correctly filtered settings.xml. How is this possible?
Thanks for your help!
Although it's not an optimal solution i found a workaround to fix my problem. Therefor i adjusted the project's deployment assembly settings in the way that the WEB-INF-Folder of the target-Folder will be taken for deployment.
You can do this by rightclicking your project and choose "Properties". Here's a screenshot of how i adjusted the settings:
Web_Deployment_Assembly.PNG

Maven with TestNG and Selenium, TestNG file being ignored

Story: So I am running a WebDriver2 test suite with TestNG and all that's bundled up in a Maven architecture. I built everything in Eclipse project first, then converted this over to a Maven project. I am not a programmer but I can hack my way through Java, I am new to Maven but pretty good with TestNG and Selenium and from everything I have researched I am approaching this correctly, I have to be missing something stupid.
Problem: I am running this all in Eclipse with Maven plugin, when I run POM as TEST I get an error:
org.testng.TestNGException: Parameter 'dataMode' is required by #Configuration of method prepareDriver but has not been marked #Optional or defined
So POM setup thusly for SureFire to grab my file, testNG dependency also set and appears to work since my error is coming from TestNG itself:
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src\test\resources\testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
[...]
</plugins>
I have also passed in the parameters to this plugin as well:
<systemPropertyVariables>
<dataMode>Custom</dataMode>
</systemPropertyVariables>
<systemPropertyVariables>
<log_level>DEBUG</log_level>
</systemPropertyVariables>
Everything in my TestNG file is correct, if I run as a TestNG test just from the testng.xml everything runs like it is supposed to, if you want that code I will update but that part works, parameters are in there correctly.
Also, when I execute the POM as TEST I get a report that 2 out of 11 tests failed... I don't have 11 tests, if you go by my testNG.xml I have only 2 #Test's that it would find, I think it's counting every TestNG annotation in this portion of my test-suite...
My theory is that it's trying to run without the TestNG file and just running every TestNG annotation in any file it finds but I don't have it setup to do that, or do I?
Try something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemProperties>
<property>
</property>
</systemProperties>
</configuration>
</plugin>

War deployment to JBoss from maven

I am having a pom file with specific properties to deploy to jboss 7.10. I am running on localhost and default ports. So I am using the hard-deploy instead of deploy. The mojo plugin was always copying the war to \server\standalone\deploy\standalone\deployments\ROOT.war for which I made a workaround.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<version>1.5.0</version>
<configuration>
<jbossHome>C:\work\jboss-as-7.1.0.Final</jbossHome>
<deploySubDir>..\..\..\standalone\deployments</deploySubDir>
<serverName>standalone</serverName>
<fileNames>
<fileName>target/ROOT.war</fileName>
</fileNames>
<unpack>true</unpack>
</configuration>
</plugin>
The hard-undeploy needs a different set of configuration than the hard-deploy goal. How can I do this?
Use appropriate maven plugin with AS7
http://docs.jboss.org/jbossas/7/plugins/maven/latest/

Tomcat 7 maven plugin additional context on tomcat7:run

When I run my web app inside eclipse using tomcat 7 maven plugin, I want an additional context to be deployed to tomcat. On the production enviroment this context is mapped to a directory outside tomcat dir using a context configuration
<Context path="/userimages" docBase="C:/test/userimages">
</Context>
And by this way is available in
http://wwww.myhost.com/userimages/test.jpg
How I achive the same on the development enviroment of the webapp (eclipse, tomcat7 maven plugin)?
In other words I want the contents of that folder to be accessible through
http://localhost:8080/userimages
or
http://localhost:8080/myapp/userimages
You should configure tomcat7-maven-plugin plugin.
Try this way:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>your_app_context_path</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
Then all your urls should start with http://wwww.myhost.com/your_app_context_path/...
More optional parameters for tomcat7:run goal can be found at apache tomcat maven plugin document
I found a workaround that doesn't do exactly what I originally wanted (publish another context using the tomcat maven plugin) but it solves my problem in a way. I add a folder "userimages" in the webapp folder of the application and this folder is used when developing. I prevent this folder from getting in the war and thus in the production server by using the "maven-war-plugin" with the following configuration in the pom.xml
<build>
....
<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<packagingExcludes>userimages/</packagingExcludes>
</configuration>
</plugin>
....
</plugins>
....
</build>
Check also here and here

Eclipse auto-build output interacts with Maven command-line build output

Since both use the target directory, Eclipse's build output sometimes interferes with the output of mvn builds run at the command line.
What's the best way to separate the two outputs?
Insert the following into your pom.xml. Eclipse's "m2e.version" property will activate the following profile which alters the location of the Eclipse build
<profiles>
<profile>
<id>IDE</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<!-- Put the IDE's build output in a folder other than target, so that IDE builds don't interact with Maven builds -->
<directory>target-ide</directory>
</build>
</profile>
</profiles>
Official way is presented here:
http://wiki.eclipse.org/M2E_FAQ#How_to_configure_Maven_project_to_use_separate_output_folders_in_Eclipse
I personally don't do something like this. Usually I basically disable auto-build in Eclipse since most builds I do from the console anyway. But if you really want it, here you are.
If you use maven-eclipse-plugin instead of M2Eclipse, here's the definition you want in order to change the Eclipse output directory:
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<buildOutputDirectory>target-eclipse/classes</buildOutputDirectory>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>