CATALINA_HOME and Maven + LOG4J - maven

I have this line in log4j:
log4j.appender.FILE.File=${catalina.home}/logs/debug.log
Works perfectly when I run the project from IntelliJ.
But when I try to run a TestNG test (from maven) it fails:
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /logs/debug.log (No such file or directory)
I could hardcode the path and all will be good. But I don't want that solution since I can deploy on various systems where ${catalina.home} is in different place.
I develop on a mac and deploy on freebsd and centos. Tomcat is in different places all the time. I could use /var/log/myapp.log but ...
Is any way to define a common variable (available in IntelliJ and when I run the maven test) with the log file path?

Please try to use the Maven Profile which will be activated when the ${env.catalina.home} is not existed, together with the Maven Surefire Plugin:Using System Properties.
<profile>
<id>mock-catalina-home</id>
<activation>
<property>
<name>!env.catalina.home</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<encoding>UTF-8</encoding>
<systemProperties>
<property>
<name>catalina.home</name>
<value>PATH_TO_CATALINA_HOME</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Please note that the PATH_TO_CATALINA_HOME can be referred by the Maven Properties as well. e.g.
<systemProperties>
<property>
<name>PATH_TO_CATALINA_HOME</name>
<value>${my.dev.catalina.home}</value>
</property>
</systemProperties>
This will help us to define the ${my.dev.catalina.home} to be various values.
I hope this may help.

In order for the log4j properties file get the correct value for ${catalina.home} when running the tests from maven, it needs to be in a file that is filtered by maven (src/main/resources is a directory for files like that). Also, the variable 'catalina.home' needs to be setup in maven. You can create a variable AKA maven property that uses an environment variable so you can define the different location for the tomcat install on each machine:
<properties>
<catalina.home>${env.catalina.home}</catalina.home>
<properties>

You can also specify the location of the log files in relation to the home directory for your user account.
In log4j this would use the following format: ${user.home}/weblogs/log4j1.log
In log4j 2, this would use the following format: ${sys:user.home}/weblogs/log4j2Rolling.log
log4j 2 can use the Java system properties, environmental variables, or Maven properties

Related

Websphere 9 maven remote deployment

My springboot application builds into a WAR file (using Jenkins). I want to automate the remote deployment to Websphere 9.
I have read around and it seems there is no maven plugin for deployment to websphere 9 but ant support is pretty good. So, I'm using maven ant plugin to help running those ant tasks. I started with attempt to list the applications installed, just to see if it works. However I'm running into an exception related to localization:
[ERROR] C:\DEV\ant-was-deploy.xml:81:
java.util.MissingResourceException: Can't find bundle for base name
com.ibm.ws.profile.resourcebundle.WSProfileResourceBundle, locale
en_US
My ant-was-deploy.xml is referenced from pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>id123</id>
<phase>clean</phase>
<configuration>
<locales>es</locales>
<target>
<ant antfile="${basedir}/ant-was-deploy.xml">
<target name="listApps"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
ant-was-deploy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="websphere" default="listApps" basedir="." >
<target name="listApps" >
<taskdef name="wsListApps" classname="com.ibm.websphere.ant.tasks.ListApplications" classpath="${wasHome.dir}/plugins/com.ibm.ws.runtime.jar" />
<wsListApps
profileName="AppServ01"
wasHome="C:\\opt\\IBM\\WebSphere\\AppServer"
/>
</target>
</project>
I think the error comes from com.ibm.ws.runtime.jar. Inside it has WSProfileResourceBundle.class and WSProfileResourceBundle_en.class but not WSProfileResourceBundle_en_US.class (name is just an assumption - I have copied the bundle with this name inside the jar but it didn't work).
I also tried to set the locale for the entire plugin but it seems that localization for this plugin is not implemented properly (no impact in my case - I set the locale to 'es' but still got the error for en_US).
I also tried to pass system parameters to maven command: mvn clean -Duser.language=fr -Duser.country=FR
It didn't work either.
So, my question is if there is a way to change the locale before the ant script? If I can set it to 'en' probably it will find the right resource bundle.
I'm fairly new to Websphere, if there is another solution to automate the remote deployment to websphere 9 I would be happy to hear it. I would rather not use scripts on target server or Jenkins plugin but if there is no other way ...
I just had the same issue. In my case, i was using an AppServer name (AppSrv1 instead of AppSrv01) that did not exist anymore, in my maven settings.xml.
The right server name solved the issue.

Cannot import correctly maven properties with PropertiesFactoryBean and PropertyPlaceholderConfigurer in spring

I'm facing problems with a jdbc dynamic properties configurer. I try to explain what exactly the problem is.
When I do mvn clean install and right after I deploy the applications in my server (Weblogic 10.3.3), everything is correct, and all the applications work fine. But, every morning, when I try to redeploy the same applications, it was shown an error message like this:
Error creating bean with name 'path.to.my.bean.JDBCPropertiesFactoryBean#6015a10' defined in class path resource [spring/configuration/placeholder-jdbcproperties.xml]: Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
This application.version comes from maven pom.xml:
<properties>
...
<application.version>MyVersion</application.version>
...
</properties>
The bean is:
<bean id="jdbcPlaceholderConfig"
class="path.to.my.bean.DefaultPropertyPlaceholderConfigurer"> <!-- Class to extend PropertyPlaceholderConfigurer -->
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="properties">
<bean class="path.to.my.bean.JDBCPropertiesFactoryBean"> <!-- Class to extend PropertiesFactoryBean -->
<property name="query">
<value>
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
</value>
</property>
<property name="dataSource" ref="ref.to.datasource.bean"/>
</bean>
</property>
So, every morning I have to rebuild with maven, and the loop starts again.
Additional information: I try to use JRebel too, but I'm not sure where can be the problem, maybe this is relevant.
Thanks in advance.
UPDATE:
This how I generate the rebel.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've just realized that with <executions>...<goal>generate</goal>...</executions>, when I do mvn clean install, without jrebel:generate, the rebel.xml files are always generated, so maybe I have to delete the executions tag, and generate the rebel.xml files once with jrebel:generate, and then, edit the rebel.xml and do again mvn clean install.
Would be that correct?
Thanks.
UPDATE WITH THE SOLUTION:
This is the final version of maven jrebel plugin in the pom.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<!-- executions tag out! to not regenerate files always -->
</plugin>
</plugins>
</build>
To create the rebel.xml:
mvn jrebel:generate
Then, if we want, we can modify the rebel.xml files if we want to exclude some files, like *.properties, as Henri's answer.
And that's it!
This can happen if you're using resource filtering with JRebel, as the application looks up the bean's xml in its unfiltered form from the project working directory (as per rebel.xml).
To resolve this, you'll need to update rebel.xml for that module, adding exclude for that particular XML file - see here.
Example

Maven Multiproject Profile activation via property

I have a problem to activate a profile. There is a master pom with the profile:
<profile>
<id>EntityUpdater</id>
<activation>
<property>
<name>entityupdater.start</name>
<value>true</value>
</property>
</activation>
....
In my childpom (jar package) I specify the property:
<properties>
<!-- ENTITY UPDATER CONFIG -->
<entityupdater.start>true</entityupdater.start>
....
But in the build process the profile does not start.
I would be very happy if anyone could help me.
Best regards, Daniel
Unfortunately, Maven can only trigger property based profiles, if you are passing them in from the command-line as JVM args:
mvn clean package -Dentityupdater.start

Maven: include resource file based on profile

I'm converting an Ant webapp project over to Maven. I have most of it working, but I'm stuck trying to figure out how to copy some resource files from different sources based on the profile.
I have src/main/resources/persistence-{dev, prod}.xml. One of these needs to be included in the war file as WEB-INF/classes/META-INF/persistence.xml.
I would like the dev version to be copied when the dev profile is active, and the prod version when prod is active.
Just use the maven resources plugin like so http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html and have a property for the file name or extension set in a profile.
If you are not wedded to the paradigm of having 3 separate persistence.xml files and copying one or the other selectively, you can use maven profiles with filtering like this (just implemented this the other day and today came across your post):
In persistence.xml:
<property name="hibernate.show_sql" value="${hibernate.debug}" />
<property name="hibernate.format_sql" value="${hibernate.debug}" />
In pom.xml create a profile and define the variable:
<profiles>
<profile>
<id>hib-debug</id>
<properties>
<hibernate.debug>true</hibernate.debug>
</properties>
</profile>
</profiles>
define a default for when you build without specifying a profile:
<properties>
<hibernate.debug>false</hibernate.debug>
</properties>
and.... make sure you turn on resource filtering:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Then you build with mvn -Phib-debug and voila! Substitution is done.

Jetty Maven Plugin is ignoring custom webdefault.xml

I'm trying to get around the common issue of Jetty locking static files on Windows with the technique of setting useFileMappedBuffer to false in webdefault.xml. Unfortunately, every time Jetty is not picking up my customized webdefault.xml.
I'm using Apache Maven 3.0.2. I've tried using the maven-jetty-plugin (v6.1.26) and jetty-maven-plugin (v8.0.0.M2) but with no difference. I've tried running clean and rebuilding as well before running Jetty.
I've verified each time that my webdefault.xml was taken from the same version as the plugin and has the correct settings, namely, only changing this setting from true to false:
...
<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>
...
And here's what my pom.xml Jetty plugin section looks like:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
</configuration>
</plugin>
I've also tried altering the path to my file:
<webDefaultXml>${basedir}/src/main/resources/webdefault.xml</webDefaultXml>
Everywhere I've seen this exact solution and it sounds like it is working for others (although I found one instance where someone had my issue). The startup for jetty has this in the output:
> mvn jetty:run
...
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
...
This further makes me think it isn't being applied. All the other paths are correct in the output.
My most direct issue that I'm seeing while Jetty is running is that whenever I edit a static file (JavaScript, CSS, etc.) with IntelliJ IDEA 10, I get this error message:
Cannot save file:
D:\...\... (The requested operation cannot be performed on a file with a user-mapped section open)
After I stop Jetty then it saves just fine. This happens every time.
Any ideas what I could be doing wrong? Thanks in advance.
I found an entirely different doc for the newer Jetty plugin jetty-maven-plugin (v8.0.0.M2) and it looks like the configuration name has changed:
http://wiki.eclipse.org/Jetty/Reference/webdefault.xml#Using_the_Jetty_Maven_Plugin
<project>
...
<plugins>
<plugin>
...
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webAppConfig>
...
<defaultsDescriptor>/my/path/to/webdefault.xml</defaultsDescriptor>
</webAppConfig>
</configuration>
</plugin>
...
</plugins>
...
</project>
This now seems to work for the newer plugin. I'm still unsure why the v6 plugin does not pick up the customized config.
The only solution I found that worked with maven-jetty-plugin 6.1.24 was this:
http://false.ekta.is/2010/12/jettyrun-maven-plugin-file-locking-on-windows-a-better-way/
The Jetty documentation outlines three ways to do it (as of Jetty 9):
https://www.eclipse.org/jetty/documentation/current/troubleshooting-locked-files-on-windows.html
I successfully used the init-param method in Maven:
<!-- Running an embedded server for testing/development -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.9.v20180320</version>
<configuration>
<webApp>
<_initParams>
<org.eclipse.jetty.servlet.Default.useFileMappedBuffer>false</org.eclipse.jetty.servlet.Default.useFileMappedBuffer>
</_initParams>
</webApp>
</configuration>
</plugin>

Resources