Copy compiled jar into %TOMCAT%/lib automatically using Maven - maven

I would like to know if there is a good way to build JAR files with Maven and after that copy them into %TOMCAT%/lib folder.
What I want to achieve is that every time when I deploy my WAR in the cloud (using Jenkins and Cloud Foundry), I get all JARs needed by Tomcat copied in its lib folder.
My use case is, my application uses Redis to store sessions (to be cloud ready), and I have added this configuration to %TOMCAT%/context.xml file
<!-- session persistence is handled by Redis Data Server -->
<Valve className="com.gopivotal.manager.SessionFlushValve" />
<Manager className="org.apache.catalina.session.PersistentManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60">
<Store className="com.gopivotal.manager.redis.RedisStore" />
</Manager>
Sessions are stored like a charm when I copy manually redis.jar, commons-pool.jar and jedis.jar in %TOMCAT%/lib, but I want to do that automatically based on the best practice.

Related

Adding security websphere.javaee.security jar to Liberty Server classpath

I am attempting to use a third party application to instrument Liberty Server 18.0 and attempting to add a library to the liberty server classpath on Linux. The library is called: "com.ibm.websphere.javaee.security.1.0_1.0.21.jar" and it is under the "/wlp/dev/api/sec" of the Liberty Server.
In my defaultServer directory I have tried several things including in my jvm.options file:
-Dorg.osgi.framework.bootdelegation=META-INF.services,com.singularity.*,com.ibm.*
-Djava.security.policy=/opt/wlp/usr/servers/defaultServer/server.policy
-Xbootclasspath/p:/opt/wlp/dev/api/spec
-javaagent:/opt/myserveragent/javaagent.jar
I've validated that my server is taking that classpath argument but it seems to have no affect. I've also attempted to add this folder also to my server.xml
<library id="agent">
<fileset dir="/opt/wlp/dev/api/spec" includes="*.jar" scanInterval="5s" />
</library>
In desperation I've also tried to simply copy the jars from the /dev/api/sec/ folder to the /wlp/lib directory which appears to be the default folder where all the main liberty classes are loaded from..
Whenever I run:
lsof -p xxxx
I can see that it is never loaded. How do I add this jar to my defaultServer classpath so my third party library can use it?
The best way to do this is to add it to the jvm.options file of your server instance:
-Xbootclasspath/p:/opt/wlp/lib/com.ibm.websphere.security_1.1.21.jar
However; as both Andy and Alasdair mentioned in the comments it seems to affect the admin-Center feature. So not recommended!!

how to set java class loader PARENT_LAST

i have a spring mvc web application that I need to change the class loader on. I need to change the class loader to be equal to PARENT_LAST. I am using WAS 6.1 and already have a jacl script from a previous web application I can copy to do the job.
In the last application Apache ant was used and what they did was to make the deploy dependent on running the jacl script.
In my new web application I am using maven install to create a war file and am deploying that war file to my application server.
How can I set the class loader to be PARENT_LAST using maven? I know how to do it in the console but if there was a way to do it using scripting that would be nice.
Also will this setting be placed somewhere in the war file so that on deploy of the application the setting will be picked up. This question comes from my lack of understanding of how jacl scripts work?
thanks
If you are only deploying the WAR file itself you can't control this, but if you have your WAR file in an EAR file you can use the deployment.xml solution. The deployment.xml file would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_1347529484613">
<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1347544766353" startingWeight="99" warClassLoaderPolicy="SINGLE">
<modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_1347543866613" startingWeight="1" uri="YourWebApp.war" classloaderMode="PARENT_LAST"/>
<classloader xmi:id="Classloader_1347543866613" mode="PARENT_LAST"/>
</deployedObject>
</appdeployment:Deployment>
Once you are done all you need to do is to add the file in the correct location of your EAR project build assuming you are using src/main/application that would be src/main/application/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml and build the EAR using Maven as normal.
During server deployment this will be picked up by WAS.
AFAIK there is no way to preconfigure WAR for PARENT_LAST during assembly. Classloading policy is specified during deployment, thus the way of setting it depends on how application is deployed.
Changing the policy using the script is straightforward. Scripts are run using wsadmin tool. The Jython snippet below does the job. It can easily be converted to Jacl.
dep = AdminConfig.getid('/Deployment:app_name/')
depObject = AdminConfig.showAttribute(dep, 'deployedObject')
classldr = AdminConfig.showAttribute(depObject, 'classloader')
AdminConfig.modify(classldr, [['mode', 'PARENT_LAST']])
AdminConfig.save()
Websphere uses deployment.xml file to govern deployment setting of each module in an ear file. You can change the classloader setting in deployment.xml at the following path:
/MyTestEAR/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml
I do not know how you can configure that in Maven.

Same webapp on Tomcat with different log4j config for each

I've hunted and read, and I think there's no way...but just in case.
I need to deploy the same webapp to Tomcat multiple times, each with a different config that indicates which database to work with. I've figured out how to do that without modifying the contents of the war file.
In short, I copied WebApp.jar to WebAppDB1.jar, WebAppDB2.jar, and deploy these to the webapps folder, and use a custom context configuration for each in tomcat/conf/Catalina/localhost. So I'm good there.
But I would really like for each of these to log to a separate file. As it is, everything goes to catalina.out. It's a Spring app using log4j and slf4j. Every avenue I've explored gets me nowhere.
For now, I'm back to updating the actual war file, going into WEB-INF/classes and updating log4j.xml, but that makes it a manual process.
Any ideas?
You can put the log4j.xml files somewhere in the outside the webapp and use Tomcat's VirtualWebappLoader to load different log4.xml for each webapps.
http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation
It looks like this.
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB1.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB1" />
</Context>
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB2.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB2" />
</Context>
And then put log4j.xml files into /somewhere/WebAppDB1 and /somewhere/WebAppDB2.
Hope this helps.

SpringSource IDE does not use project name as root URL for Spring MVC application

When I create a Spring MVC Template Project with the SpringSource IDE, I run the application and the root URL is set as the last word of the default package name:
For example, when I create the project, I set the default package as com.sample.myapp. When I run the application, it opens at http://localhost:8080/myapp. Why does the root URL not use my project name, MyProject, instead?
This is a problem because I have to specify all of the URL's in my application to /myapp/resources/css/mycss.css but when I export a .war and deploy it to a Tomcat server, then Tomcat expects the project name instead (it wants me to use /MyProject/resources/css/mycss.css. As a result, all of my links are broken when I deploy (but not when I run on tomcat locally within the SpringSource IDE...)
Has anyone else run into this problem?
Example for comment discussion below:
<bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
<property name="service"
value="https://localhost:8443/MyProject/j_spring_cas_security_check" />
<property name="sendRenew" value="false" />
</bean>
It depends on how you run the application. I assume you chose "Run on Server" from within the SpringSource IDE (STS)? If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.
Hope that helps.
The first part in the URL after host and port is called the context-path. In your case myapp. In tomcat the context-path is equal to the name of the war-file. If you name the war-file ROOT.war then no context-path is used.
As bimsapi mentioned you can change the context-path in STS, too.
But: You can and should make your app independent of the context-path. If you are using JSP then build links with the <c:url /> tag:
<c:url value="/resources/css/mycss.css" />
This outputs the correct url including the actual context-path. Or your can store it in a variable and use it later:
<c:url value="/items/index" var="cancel-url" />
Cancel
In Java code you can get the context-path with:
request.getContextPath()
where request is the current HttpServletRequest object.
Using this makes your app completely independent of the actual context-path which simplifies deployment a lot.
Not sure about STS, but the war file name is set in pom file :
</dependencies>
<build>
<finalName>yourProject</finalName>
(just realized you didn't specify maven, oh well)

Do i need a context.xml file to deploy spring webapp to tomcat

I am new to Tomcat, so I apologize if this is a dumb question. I have created a spring mvc webapp that currently runs locally using the maven-jetty-plugin.
I can successfully create the WAR file. I would like to deploy the WAR file into a tomcat6 instance. However, I am not sure if I need to create a context.xml file for tomcat? And if I do, where would I place the file in my spring webapp? My current directory structure looks like this:
src
|
|-main
| |-java
|-resource
| |-META-INF
|-webapp
| |-WEB-INF
|-web.xml
The context.xml file is used to configure application specific instructions for your container (tomcat). For instance, you can define JNDI resources, loggers, valves, etc. See Context Container for more details.
By default, Tomcat will auto-generate a default context.xml for your war if you do not specify one yourself and store it within its own configuration files in /conf/[enginename]/[hostname]/[context-root].xml
If you want to include it as part of your war, you can place it in /META-INF/context.xml within your war.
No, you don't. The context.xml file is optional, and by no means required by Spring.
context.xml is used to configure Tomcat itself. The defaults are sensible, there's no need to override them unless you have a good reason.

Resources