Tomcat-maven-plugin 401 error - maven-2

i am learning tomcat basics and while i tried to deploy my web-application on tomcat i am getting the following error
[ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project struts2-demoapp: Cannot invoke Tomcat manager: Server returned HTTP response code: 401 for URL: http://localhost:8080/manager/html/deploy?path=%2FmkyWebApp&war= -> [Help 1]
[ERROR]
as per this it seems war file location is not being passed to the tomcat manager.i have the following entries in my tomcat-user.xml
tomcat-users>
<user name="admin" password="admin" roles="admin,manager" /><!--
NOTE: The sample user and role entries below are wrapped in a comment
and thus are ignored when reading this file. Do not forget to remove
<!.. ..> that surrounds them.
-->
<role rolename="manager"/>
<role rolename="admin"/>
<user username="admin" password="admin" roles="admin,manager"/>
</tomcat-users>
here are the details of the pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<warFile>${project.build.directory}/${project.build.finalName}.war</warFile>
<url>http://localhost:8080/manager/html</url>
<server>myserver</server>
<path>/mkyWebApp</path>
</configuration>
</plugin>
</plugins>
</build>
in my setting.xml there are the entries
<server>
<id>Tomcat6.x</id>
<username>admin</username>
<password>admin</password>
</server>
i am not sure what exactly is going wrong here.any help in this regard will be helpful.

Change
<server>
<id>Tomcat6.x</id>
<username>admin</username>
<password>admin</password>
</server>
to
<server>
<id>myserver</id>
<username>admin</username>
<password>admin</password>
</server>
If you are using tomcat 7 use
<url>http://localhost:8080/manager/html</url>
If tomcat 6
<url>http://localhost:8080/manager</url>
start tomcat run tomcat7:deploy or tomcat6:deploy

You need to map the credentials from your settings.xml to the server configuration at your pom.xml.
In your case, this is done but setting the <id> element of your server, to match the server's host name from the pom.xml.
Since you are pointing localhost, the id must be also localhost.
When you change the hostname, you must also update settings.xml.

It's in the plugin configuration docs: the server/id tag in Maven settings must match the configuration/server value in your POM file, i.e. put <server>Tomcat6.x</server> in POM file.
There are some other minor issues with your tomcat-maven-plugin entry in the POM file:
you are missing the <version>1.1</version> tag,
the /html suffix in the Tomcat manager URL is unnecessary (cf. the default value for <url> tag).

When I was also running into this problem. My issue was using the older
<groupId>org.codehaus.mojo</groupId>
instead of using
<groupId>org.apache.tomcat.maven</groupId>
My setup is as follows
~/.m2/settings.xml
<settings>
<servers>
<server>
<id>localhost</id>
<username>tomcat</username>
<password>tomcat</password>
</server>
</servers>
</settings>
pom.xml
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager</url>
<server>localhost</server>
<path>/myapppath</path>
</configuration>
</plugin>
tomcat/conf/tomcat-users.xml
<tomcat-users>
<role rolename="manager"/>
<user username="tomcat" password="tomcat" roles="admin-gui,manager-gui,manager-script,tomcat,manager"/>
</tomcat-users>

I Advise you to use this plugin :
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.2</version>
It's very helpful with Tomcat7. I Have the same issue with mojo <groupId>org.codehaus.mojo</groupId>
but now, using Cargo plugin, the deploy run smooth as silk.

Related

Where is Cargo generating context XML for Jetty 6.x?

I am trying to implement the solution mentioned in How to specify jetty-env.xml file for Maven Cargo plugin for Jetty?
However I am facing something even more fundamental: My Cargo is simply not generating any context xml.
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<!-- Container configuration -->
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
</container>
<!-- Configuration to use with the container or the deployer -->
<configuration>
<properties>
<cargo.servlet.port>${itest.webapp.port}</cargo.servlet.port>
<cargo.jetty.createContextXml>true</cargo.jetty.createContextXml>
</properties>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>myApp-web</artifactId>
<type>war</type>
<properties>
<context>/myApp</context>
</properties>
</deployable>
</deployables>
<!--
<configfiles>
<configfile>
<file>${project.build.outputDirectory}/jetty-env.xml</file>
<todir>contexts</todir>
<tofile>${jetty6.context}.xml</tofile>
</configfile>
</configfiles>
-->
</configuration>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
The basic idea is, we are providing the a custom context.xml to replace the one generated. However, when I am trying out, I cannot find any context XML generated by Cargo (Please note that I have remarked the custom config files, and with cargo.jetty.createContextXml being true)
I am not sure if it is my problem in setting causing the context not generated, or the context is generated somewhere I overlooked. I have checked under target/cargo/ and the temp directory that cargo expanded my WAR, neither place contains the context xml.
(I am using Maven 2.2.1, Cargo 1.2.1, JDK 6)
I am not 100% sure what your problem is, but here is what cargo does on my system for Jetty6.
The directory where the Jetty installation is NOT where the runtime context and webapp files are. In my case, they are stored in the Java temp directory (i.e. java.io.tmpdir). On my Ubuntu system this is /tmp. Under this directory, there is a cargo/conf directory. Under /tmp/cargo/conf I have a contexts directory where the context.xml file is stored -- although the actual name of the file is never context.xml it is always named after the web app context.
In my case, this file is given the same name as the context I configured cargo with. Herein may lie your problem because I noticed that you did not supply a context as I do:
<deployables>
<deployable>
<properties>
<!-- Web root context URL -->
<context>${build.appserver.context}</context>
</properties>
</deployable>
</deployables>
Secondly, I also noticed you have commented out the section that places the context.xml file in the right place. Unless you uncomment that, this isn't going to work.
Thirdly, did you set the value of the ${jetty6.context} Maven property?
Fourthly - I think for this to work you need to use a standalone configuration of Jetty. This shouldn't be a problem as Cargo will automatically download and install it for you. See my config here:
<container>
<containerId>jetty6x</containerId>
<!-- Using Jetty for build portability so type != "remote". For Jetty
would prefer type = "embedded" but we must go with "installed" because jetty-env.xml
file would be ignored. See http://jira.codehaus.org/browse/CARGO-861 -->
<type>installed</type>
<zipUrlInstaller>
<url>http://dist.codehaus.org/jetty/jetty-6.1.26/jetty-6.1.26RC0.zip</url>
<installDir>${build.working}</installDir>
</zipUrlInstaller>
<dependencies>
<!-- The following dependencies are added to the servlet container's
classpath as if they were installed by a system admin. In order to be included
here, they need to be listed as dependencies in this pom.xml. -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc5</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
</dependency>
</dependencies>
</container>
<!-- Do not hang and wait for a client, just do it -->
<wait>false</wait>
<configuration> <!-- Deployer configuration -->
<!-- Running Jetty container with type=installed (e.g. local) so
type != "runtime", and we are installing it during this execution for the
sake of portability so type != "existing" -->
<type>standalone</type>
<properties>
<!-- Use the port number from settings.xml -->
<cargo.servlet.port>${build.appserver.port}</cargo.servlet.port>
</properties>
<deployables>
<deployable>
<properties>
<!-- Web root context URL -->
<context>${build.appserver.context}</context>
</properties>
</deployable>
</deployables>
<configfiles>
<configfile>
<file>${basedir}/target/jetty-context.xml</file>
<todir>contexts</todir>
<tofile>${build.appserver.context}.xml</tofile>
</configfile>
</configfiles>
</configuration>

GlassFish v3 cargo-maven2-plugin

I see that supports GlassFish v3, but the online examples is sparse. I continue to get the same error back from cargo:
Cannot find the GlassFish admin CLI JAR: admin-cli.jar
Here is my pom
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.3</version>
<configuration>
<container>
<containerId>glassfish3x</containerId>
<type>installed</type>
</container>
<configuration>
<type>standalone</type>
<home>C:\glassfishv3</home>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8082</cargo.servlet.port>
<cargo.remote.username></cargo.remote.username>
<cargo.remote.password></cargo.remote.password>
</properties>
</configuration>
<deployer>
<type>installed</type>
<deployables>
<deployable>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
corrected Pom:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.3</version>
<configuration>
<container>
<containerId>glassfish3x</containerId>
<type>installed</type>
<home>C:\glassfishv3</home>
</container>
<configuration>
<type>standalone</type>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8082</cargo.servlet.port>
<!-- if no username/password don't use these, it will fail
<cargo.remote.username></cargo.remote.username>
<cargo.remote.password></cargo.remote.password> -->
</properties>
</configuration>
<deployer>
<type>installed</type>
<deployables>
<deployable>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
In your Glassfish installation, do you have the admin-cli.jar file present in modules directory?
For more information about this module, check this link.
Edit
It seems that you have a problem in your configuration. As you can see here, there are several <home> nodes that can be used in the <configuration> of the Cargo plugin.
If you define the <home> inside the <configuration> tag, like you do in your pom.xml, this tag is used for:
For standalone configuration this is the location where Cargo will create the configuration and for existing configuration this is where it is located
However, in your case, you must move the <home> in the <container> tag. As described in the link above, this <home> is used for:
Location where the container is installed.

Additional context for tomcat:run goal

How do I add an additional directory based context to a tomcat:run configuration?
I have the following jetty plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextHandlers>
<contextHandler implementation="org.mortbay.jetty.handler.ContextHandler">
<contextPath>/media/data</contextPath>
<resourceBase>/somedir/media/data</resourceBase>
<handler implementation="org.mortbay.jetty.handler.ResourceHandler" />
</contextHandler>
</contextHandlers>
<contextPath>/</contextPath>
<webAppSourceDirectory>foo-project/target/foo-webapp</webAppSourceDirectory>
...
</configuration>
</plugin>
</plugins>
...
</build>
How do I do this with tomcat:run?
I have a context.xml file in the tomcatconf dir
<?xml version='1.0' encoding='utf-8'?>
<Context path="/media" docBase="/somedir/media"/>
but this seems to get ignored.
I've also tried to explicitly set the contextFile parameter in the plugin configuration, but to no avail.
Reference:
http://mojo.codehaus.org/tomcat-maven-plugin/run-mojo.html
have a look at the deployment description of the tomcat plugin.
Place the context.xml file to the default location
src/main/webapp/META-INF/context.xml
or use tomcat:run with a contextFile property on the commandline
mvn tomcat:run -DcontextFile="<path-to-your-context.xml-file>"

Cargo not working over proxy integrated with maven 2

I have integrated Cargo plugin in my maven 2 project POM.xml.
During hot deployment I am unable to connect to my Tomcat container that is available across a proxy. My maven settings.xml already contain proxy setting but cargo is not picking it up.
I tried defining proxy settings for Cargo plugin expilicitly but that too didn't worked.
My plugin xml for Cargo is as:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<!--<version>1.0.1-alpha-1</version>-->
<version>1.0-beta-1</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.proxy.host>xxx.xxx.xxx.xxx</cargo.proxy.host>
<cargo.proxy.port>xxxx</cargo.proxy.port>
<cargo.hostname>xxx.xxx.xxx.xxx</cargo.hostname>
<cargo.protocol>http</cargo.protocol>
<cargo.servlet.port>80</cargo.servlet.port>
<cargo.tomcat.manager.url>http://xxx.xxx.xxx.xxx/manager</cargo.tomcat.manager.url>
<cargo.remote.username>xxxxxxx</cargo.remote.username>
<cargo.remote.password>xxxxxxx</cargo.remote.password>
</properties>
</configuration>
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupId>Test</groupId>
<artifactId>Test</artifactId>
<type>war</type>
<!--
<properties> <context>optional root context</context>
</properties> <pingURL>optional url to ping to know if deployable
is done or not</pingURL> <pingTimeout>optional timeout to ping
(default 20000 milliseconds)</pingTimeout>
-->
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
Please help.
Thanks in advance.
Ashish
I may be wrong but I don't think Cargo support this. But, as the remote deployer for Tomcat uses the manager application and thus HTTP, try to set proxy settings at the JVM level by passing properties on the command line when invoking maven:
mvn cargo:deploy -Dhttp.proxyHost=<hostname> -Dhttp.proxyPort=<port>
Or use the environment variable MAVEN_OPTS:
export MAVEN_OPTS="-Dhttp.proxyHost=<hostname> -Dhttp.proxyPort=<port>"
Hopefully this issue with proxy will be fixed in Cargo 1.1.0

install war file at server deploy directory

I'm want to have the war file deployed in the server deploy directory (or any directory of my choice) along with the one deployed in the repository. Also, can I control the name of the war file deployed like, I don't want the war file to be projectname-1.0.war I just want the name of the war file be projectname.war.
Thanks,
Ravi
Thanks guys,
I got it working. here is what I did.
I added this in my pom.xml file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>mavenproject1</warName>
<outputDirectory>C:\jboss-5.1.0.GA\server\default\deploy</outputDirectory>
</configuration>
</plugin>
</plugins>
This solved both my naming and placing the war file.
Ravi
A first option would be to use the JBoss Maven Plugin that allows to start/stop JBoss and deploy/undeploy applications via JMX.
Your configuration must set the location to your JBoss Home directory. This can be done by setting the home directory with the jbossHome tag in the plugin configuration:
<project>
...
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<jbossHome>C:/jboss-5.1.0.GA</jbossHome>
</configuration>
</plugin>
...
</plugins>
...
</build>
...
</project>
Then, just use one of the goal defined here, like:
$ mvn jboss:deploy
Another option would be to use the Cargo Maven Plugin. Below an example of plugin configuration that you could add to your war project:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<wait>false</wait>
<container>
<containerId>jboss5x</containerId>
<home>C:/jboss-5.1.0.GA</home>
</container>
<configuration>
<type>existing</type>
<properties>
...
</properties>
</configuration>
</configuration>
<plugin>
Then, to deploy a "deployable" (here, your war) to a running container:
$ mvn cargo:deploy
'Deployment' may sound very technical but its just copying file to the deployment directory. In some cases you may have to restart the server.
To change what it deploys the file as, use the tag in the build section of your pom.xml to specify the package name.
http://maven.apache.org/plugins/maven-jar-plugin/sign-mojo.html