Start a web container then deploy a war with cargo maven2 plugin - java

I tried to use cargo-maven2-plugin to automate my WAR module deployment for testing.
I wonder how can I start the tomcat server (pre-installed in my machine) and deploy my war to the started server automatically?
Documentation from Cargo project mentions that cargo:start goal can optionally deploy deployables:
http://cargo.codehaus.org/Maven2+plugin#Maven2plugin-gettingstarted
cargo:start Start a container. That task can optionally install and configure a container; it can also optionally deploy deployables (WAR, EAR, etc.) to it.
However, I have no idea how to enable this option to make it deploy deployables when running cargo:start.
Here is my current pom configuration:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0</version>
<configuration>
<wait>true</wait>
<container>
<containerId>tomcat6x</containerId>
<home>${tomcat.home}</home>
</container>
<configuration>
<type>standalone</type>
<home>target/tomcat6x</home>
</configuration>
<deployer>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>my-war</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>my-war</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
When I run "mvn cargo:start", the tomcat server will be started, however, the my-war deployable won't be deployed. And I have to run "mvn cargo:deploy" from another shell to deploy this war.

OK -- There are a few things going on here:
1) You mention that the Tomcat server is pre-installed on the machine, but yet you have the directory specified in your target directory. This is not a good idea as that will get wiped out after a mvn clean.
2) For pre-installed Tomcat you are not setting the type to existing inside the deployer configuration.
3) Where is your context definition? Do you have a context fragment file or are you using a tomcat.xml file inside the WEB-INF directory in the WAR?
Bottom line a combination of configuration errors are holding you back. Post an updated Maven configuration and addition details about the XML context definition and I can help you further.

Related

Maven:How to exclude all thirdparty jar from web project source code?

Currently we have a JSF Based web project which we are deploying in Jboss EAP 6.1 server.Its a multi-module project with plenty of third party jars added inside web-porject/lib folder.
When we run mvn install it create war file and we deploying this war file in Jboss but issue that its size is too big around 300MB.So sometime its hard for us deploy in war in the server if any single line changes in code its taking time upload the project in server.
We want we should separate out jar file(Third Party) and source code .So war size should be less and all third party jar should be seperate out from the source code.
This project is maven based project so all the dependency added in pom.xml file so for developer prospective we should not remove the pom file entry .
Can Someone please suggest what should be best way to handle this situation?
You should do that in two steps.
First exclude dependencies from packaging and use them only for compilation phase:
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
</dependencies>
In the second step put all the jars needed for running the application in jboss JBOSS_HOME/lib folder.
You can do that by using Copying project dependencies maven plugin:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Converting Eclipse dynamic web project to maven project with modules

so I just started a new project at work where I've gotten some code, really strange code...
So the project structure was as follows:
simple-java-project-1 ---> eclipse java project which holds some database-connectors
simple-java-project-2 ---> holds the logic of converting, formatting etc of the data, uses the connectors from project-1
dynamic-web-project --> just gets the jars from project1 and project2 and offer's a websocket api to send this data etc.
I would like to convert this mess to a well structured maven project. So I created a master maven project with this pom:
......bla bla bla....
<modules>
<module>the-connectors</module>
<module>the-logic</module>
<module>the-dynamic-web-module</module>
</modules>
<dependencies>.....</dependencies/>
And the modules as well. I added the dependencies needed to them.
So now the hard part I didn't get : How can I achive that the whole project is deployed to the local tomcat and runs?
The web.xml and /WEB-INF/ stuff is in the "the-dynamic-web-module" module for now,...
Any ideas or hints?Thanks in advance.
You should add the tomcat7-maven-plugin to your the-dynamic-web-module:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<systemProperties>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
After building and installing the 2 other modules (mvn install), go to the-dynamic-web-module and launch mvn tomcat7:run. Then with your browser, go to localhost:8080/ and you should get your app.

Maven dependency directory not created in target

I'm using Maven 3.3.3 to create a Spring MVC application that can be deployed to Heroku. I intend to use jetty-runner as suggested by the Heroku documentation (https://devcenter.heroku.com/articles/getting-started-with-spring-mvc-hibernate).
I can't get Maven to create the target/dependency folder in which to copy the jetty-runner.jar file. I use the commands:
mvn clean
mvn package
I get the target directory created, with all resources, web pages and source, but not the "target/dependency" folder where the jetty-runner.jar file should be copied.
My pom.xml is almost identical to the one provided by the sample application in the link above. My project and pom.xml file can be found in the following GitHub repository:
https://github.com/gfigueroa/nlp-tools-spring-heroku
Add an activation of your dependency-plugin outside the <pluginManagement> section:
...
<build>
<pluginManagement>
<plugins>
<!-- Leave as is -->
...
</plugins>
</pluginManagement>
<!-- Add the section below -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
...
The <pluginManagement> section is for declaring how your plugins should behave when activated. The activation itself is done outside this section.

Maven: how to copy artifact to specific directory?

The "install" goal copies the artifact to the target directory and to the local repository.
How can I tell Maven to copy it also to a given directory (like the deploy directory of JBoss for example).
The goal copy of maven-dependency-plugin does what you want, see the example.
It is however not a good idea to copy anything outside your target directory (or ${project.build.directory} to be precise) - especially if such action is attached to a build phase, because it introduces unexpected side-effects of the build, and sometimes even loss of reproducibility.
As #Andreas_D notes, there is a better alternative for JBoss deployment purpose; similarly for deploying to other appservers.
According to http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html you can copy the just built artifact to a specific directory:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-installed</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>some-other-place</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
If you want to copy file to a webserver (local or distant) you can use Maven upload plugin :
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-upload-plugin</artifactId>
<version>1.1</version>
<configuration>
<resourceSrc>
${project.build.directory}/${project.build.finalName}.${project.packaging}
</resourceSrc>
<resourceDest>${jboss.deployDir}</resourceDest>
<serverId>${jboss.host}</serverId>
<url>${jboss.deployUrl}</url>
</configuration>
</plugin>
And to configure parameters in a smart way, I use maven profiles :
<profiles>
<!-- local deployment -->
<profile>
<id>developpement</id>
<properties>
<jboss.host>localhost</jboss.host>
<jboss.deployDir>appli/jboss-4.0.4.GA/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>file://C:/</jboss.deployUrl>
</properties>
</profile>
<!-- distant deployment -->
<profile>
<id>validation</id>
<properties>
<jboss.host>ENV_val</jboss.host>
<jboss.deployDir>/home/envval/jboss/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>scp://PROJECT_LAN_HOST</jboss.deployUrl>
</properties>
</profile>
</profiles>
I've created an "ant launcher", to use it by clicking under Eclipse ant view :
<target name="copy war to JBoss local" description="Copy war to local JBoss">
<maven goal="upload:upload" options="-Pdeveloppement" />
</target>
But you can simply run it on a command line :
mvn upload:upload -Pdeveloppement
By the way, for distant deployment, you may need a login password for scp to work. You have to add them to you Maven settings.xml file :
<settings>
...
<servers>
<server>
<id>ENV_val</id>
<username>login</username>
<password>password</password>
</server>
</servers>
...
</settings>
The best approach would be to use a plugin which will actually deploy your application, such as cargo or jboss-maven plugin (credit to #Andreas_D for that one).
This would be a better approach to using a copy or generic upload tool since deploying is what you are actually trying to do.
With the cargo plugin you have the option to deploy to a variety of running servers. We took this approach to test locally in jetty using the jetty plugin during the build and had a profile to deploy to tomcat on demand via cargo.
Note: If you have your target server (JBOSS) installed locally on the dev box as well then you can also use cargo to start/stop your server during your build process as well. The downside of this approach is that you will need it to reference it's location in the pom file, so either all devs install it in the same location or a system property that defines where it is located (similar to JAVA_HOME).

How to deploy remotely EAR to JBoss 5.1.0.GA using Cargo maven plugin?

Has someone successfully deployed EAR remotely to JBoss 5.1.0.GA? My pom.xml configuration is as follows:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.1-SNAPSHOT</version>
<configuration>
<container>
<containerId>jboss51x</containerId>
<type>remote</type>
<timeout>600000</timeout>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.remote.username>username</cargo.remote.username>
<cargo.remote.password>password</cargo.remote.password>
<cargo.hostname>myserver</cargo.hostname>
<cargo.servlet.port>8888</cargo.servlet.port>
</properties>
</configuration>
<deployer>
<type>remote</type>
<deployables>
<deployable>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
This results in the following error message:
java.io.IOException: Server returned HTTP response code: 500 for URL:
http://myserver:8888/jmx-console/HtmlAdaptor?action=invokeOpByName&name=jboss.system:service%3DMainDeployer&methodName=deploy&argType=java.net.URL&arg0=file:d%3A%5Cear%5Cmy-ear-1.0-SNAPSHOT.ear
To my knowledge, remote deployment with JBoss doesn't work out of the box (the problem being that the file to be deployed must exist locally on the JBoss server). This issue is detailed in CARGO-416 which is still open (I have no idea if the patch can still be applied smoothly).
Someone is mentioning a dirty workaround in this thread (in short, ftping your deployable to the remote machine first) but it doesn't seem very satisfying.
Update: CARGO-416 has been fixed in Cargo 1.0.3 and remote deployments on JBoss AS is now supported and documented in the dedicated JBoss Remote Deployer wiki page.

Categories