Maven - POM: How to make the jetty port changeable so that it can be retrieved later? - maven-2

I'm working on an integration test suite and I've got a question for you.
My parent pom defines the use of the jetty plugin with the goal: run-war. I need to make the port that jetty listens on changeable via the command-line. This can be achieved by passing -Djetty.port=8099 for example.
In the child project, I need to use this port number to configure the endpoint for some SOAP tests that I'll need to run on the service hosted by jetty.
If I use ${jetty.port} in my child pom in the end-point configuration this works fine IF and only IF I explicitly pass -Djetty.port when invoking maven.
In my child pom:
<endpoint>http://127.0.0.1:${jetty.port}/{artifactId}<endpoint>
I need jetty.port to be filled in with 8080 which is what jetty defaults to if -Djetty.port is not explicitly passed, and still catch any other port values if the command line argument is specified.

Use the properties section, and add a jetty.port property with a default value:
<properties>
<jetty.port>8080</jetty.port>
</properties>

config maven jetty plugin:
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1H.14.1</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8085</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
If you want to use a newer version of jetty plugin, use the following configuration:
From http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html:
You could instead configure the connectors in a standard jetty xml config file and put its location into the jettyXml parameter. Note that since jetty-9.0 it is no longer possible to configure a https connector directly in the pom.xml: you need to use jetty xml config files to do it.
Something like:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.5.v20130815</version>
<configuration>
<jettyXml>src/main/resources/jetty.xml</jettyXml>
<webApp>
<contextPath>/yourCtxPath</contextPath>
</webApp>
</configuration>
</plugin>
would do the trick, with
jetty.xml file content:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call id="httpsConnector" name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="Server" /></Arg>
<Set name="host"><Property name="jetty.host" /></Set>
<Set name="port"><Property name="jetty.port" default="8085" /></Set>
<Set name="idleTimeout">30000</Set>
</New>
</Arg>
</Call>
</Configure>
See the log after 'mvn jetty:run', at the end should show something like:
2013-09-05 09:49:05.047:INFO:oejs.ServerConnector:main: Started ServerConnector#a6e9cb4{HTTP/1.1}{0.0.0.0:8085}
You will need to use maven 3 and java 7 for this version of plugin.

Related

How to externalize properties of persistence.xml for JBOSS 7.1.1?

My persistence.xml is currently present in the application war(in META-INF folder.). However, for the application to be run across multiple DBs the persistence needs to be changed again and again. I want to avoid it. But, I am not able to understand how will i configure the properties(like dialect) in the persistence.xml from, say, a property file which i would change based on my DB, hence not compelling me to update and redeploy my war.
My problem can also be resolved if i can configure the dialect in the datasource in standalone.xml where i have mentioned other DB details. I am not being able to make out what the property would be.
Though i would prefer a solution for the first one.
PS: I am rookie in Web App development. Questions might annoy you. :D
I use a method that works well for hibernate.
1) put the hibernate configuration properties in a xml file (call it hibernate.cfg.xml but it's not mandatory)
this is an example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.search.default.directory_provider">ram</property>
</session-factory>
</hibernate-configuration>
you can put there only hibernate properties that do not start with hibernate.ejb
2) Create a jboss module. It's very simple. Suppose you want to call the module com.myorganization.config than create a directory structure in the modules folder of you server installation: /com/myorganization/config/main. In the main folder put the hibernate.cfg.xml file and the following module.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.myorganization.config">
<resources>
<resource-root path="."/>
</resources>
</module>
3) In your persistence.xml adding the following property:
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
4) Finally, in the META-INF/MANIFEST.MF file add the following line:
Dependencies: com.myorganization.config
If you like maven use the maven-war-plugin in order to change the MANIFEST.MF:
<configuration>
<archive>
<manifestEntries>
<Dependencies>com.myorganization.config</Dependencies>
</manifestEntries>
</archive>
</configuration>
That's all.

How i can add some jvm options to arquillian test

Its possible to add some jvm options to embedded glassfish using arquillian ?
I need to add that jvm options:
-Djavax.net.ssl.keyStorePassword=changeit
-Djavax.net.ssl.trustStorePassword=changeit
Java properties on Glassfish are configured in domain.xml. Since you are running an embedded Glassfish, you don't really have a domain.xml file you could modify. You can try to do this in arquillian.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns="http://www.jboss.org/arquillian-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/arquillian-1.0 http://jboss.org/schema/arquillian/arquillian-1.0.xsd">
<engine>
<property name="deploymentExportPath">target/</property>
</engine>
<container qualifier="glassfish" default="true">
<configuration>
<property name="configurationXml">file:src/test/resources/domain.xml</property>
...
</configuration>
</container>
</arquillian>
The configurationXml property is used to pass the configuration file to use for the embedded insance. See also https://docs.jboss.org/author/display/ARQ/GlassFish+3.1+-+Embedded. domain.xml itself has a section for JVM arguments.

Tomcat-maven-plugin 401 error

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.

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