Using placeholders in Jetty contextXML file - maven

I'm using Maven and jetty plugin to local start my application.
And I have seen way that you could use placeholders in Jetty contextXML.
Here is part of my pom.xml (located in separate module) where I connected jetty plugin:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<stopKey>${test.jetty.stop-key}</stopKey>
<stopPort>${test.jetty.stop-port}</stopPort>
<webAppConfig>
<contextPath>/${test.contextPath}/*</contextPath>
</webAppConfig>
<httpConnector>
<port>${jetty.port}</port>
<host>${jetty.host}</host>
</httpConnector>
<contextXml>${project.basedir}/src/main/resources/jetty/${jetty.mode}-jetty.xml</contextXml>
</configuration>
<dependencies>
...
</dependencies>
</plugin>
Here is part of my stub-jetty.xml file:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="loggerCF" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jms/logger/connectionFactory/loggerCF</Arg>
<Arg>
<New class="org.apache.activemq.ActiveMQConnectionFactory">
<Arg>tcp://${active-mq.host}:${active-mq.port}</Arg>
</New>
</Arg>
</New>
</Configure>
Parameters "active-mq.host" and "active-mq.port" defined in main pom.xml file.
When I run I receive next exception:
Caused by: java.net.URISyntaxException: Illegal character in authority at index 6: tcp://${active-mq.host}:${active-mq.port}
at java.net.URI$Parser.fail (URI.java:2848)
...
So I understand that Jetty not understand that needed to change placeholders to values from Maven properties. How to fix this problem or what I could read about this?

You can't directly reference properties defined in Maven configurations. The desired behavior could be achieved with System Properties. You can set it up as a part of jetty-maven-plugin configuration, the required option is systemProperties or systemPropertiesFile.
Here is a rough example (please note that I haven't checked it):
<New class="org.apache.activemq.ActiveMQConnectionFactory">
<Arg>tcp://<SystemProperty name="active-mq.host"/>:<SystemProperty name="active-mq.port"/></Arg>
</New>
and pom:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<systemProperties>
<systemProperty>
<name>active-mq.host</name>
<value>${active-mq.host}</value>
</systemProperty>
<systemProperty>
<name>active-mq.port</name>
<value>${active-mq.port}</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>

Related

Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant

I have an Ant project that builds just fine on its own. I'm now trying to wrap it in a Maven build that will kick off the Ant build using maven-antrun-plugin. When I do this the build fails and I get this error,
[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Java\jdk1.8.0_65\jre"
There are a lot of SOF posts on this error but I feel like mine is unique since it only happens when I'm wrapping the Ant build in Maven i.e., I do not get this error on the same project when I just say $ ant build.
This is part of my pom.xml file
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="build" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>build/bin/myWarFile.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
My JAVA_HOME Environment Variable is set to C:\Java\jdk1.8.0_65.
The file that is mentioned in the error is from a library my work maintains where we keep all of our Jars. In that file here is what's on line 170
<target name="compile-src">
<!-- Compile source -->
<javac srcdir="${java.src.dir}"
destdir="${class.dir}"
debug="${debug.flag}"
deprecation="${deprecation.flag}"
nowarn="${warnings.flag}"
optimize="off"
source="${source.value}">
<classpath refid="compile.classpath"/>
</javac>
</target>
The line with source= is line 170.
It's a common issue. Try with this configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
<!-- Add this dependency to your ant-run configuration -->
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</plugin>
Maven uses Java's system property java.home, which is not the same as the environment variable JAVA_HOME, but it is using it to compute its java.home by tacking on the jre sub-directory, as witnessed. Consequently, stuff needed by Ant is simply not available in the jre directory.
However, this configuration ensures that Ant's plugin dependencies are correctly satisfied.
You need to point to JDK not JRE. Just remove ire and try.
It is currently set to "C:\Java\jdk1.8.0_65\jre"
And if your JDK is set - another workaround - Can you copy tools.jar from jdk lib to jre lib and see if it works.

Cargo:run If you specify a containerId, you also need to specify a containerUrl

I am trying to run tomcat though cargo. Tomcat is already installed. cargo:deploy is working fine but when I try cargo:run I get the following error
If you specify a containerId, you also need to specify a containerUrl.
If I specify a containerURL I get the following error
[ERROR] Failed to execute goal org.codehaus.cargo:cargo-maven2-
plugin:1.4.13:run (default-cli) on project ctm: Unable to parse configuration
of mojo org.codehaus.cargo:cargo-maven2-plugin:1.4.13:run for parameter
containerURL: Cannot find 'containerURL' in class org.codehaus.cargo.maven2.configuration.Container
...
This is my Maven config
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<container>
<type>existing</type>
<containerId>${cargo.maven.containerId}</containerId>
<home>${container.home}</home>
</container>
<configuration>
<type>existing</type>
<home>${container.home}</home>
</configuration>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<type>war</type>
<properties>
<context>example</context>
</properties>
</deployable>
</deployables>
</configuration>
<executions>
<execution>
<id>run</id>
<configuration>
<configuration>
<type>existing</type>
</configuration>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
this is my profile
<profile>
<id>developer-properties</id>
<properties>
<cargo.maven.containerId>tomcat7x</cargo.maven.containerId>
<container.home>C:/apache-tomcat-7.0.35</container.home>
</properties>
</profile>
According to cargo documentation; type "existing" should use an existing container installation. I don't think I need containerURL unless the type is "standalone". I don't understand why I'm getting the containerURL error.
Documentation on type is here: https://codehaus-cargo.github.io/cargo/Existing+Local+Configuration.html
I think the documentation is unclear. I think cargo:run always uses a standalone local configuration, hence the description
If the plugin configuration defines a container with a standalone
local configuration, it will create the configuration.
So it's probably ignoring your existing local configuration.

Maven antrun: pass maven properties to ant

I am trying to pass maven properties (defined through profiles) to a antrun execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<dependencies>
<!-- ... these are ok -->
</dependencies>
<executions>
<execution>
<configuration>
<target>
<property name="ant_destDir" value="${destDir}" />
<property name="ant_serverDeploy" value="${serverDeploy}" />
<property name="ant_deployDir" value="${deployDir}" />
<property name="ant_userDeploy" value="${userDeploy}" />
<property name="ant_passwordDeploy" value="${passwordDeploy}" />
<!-- correct task definitions for sshexec and scp -->
<sshexec host="${serverDeploy}" username="${userDeploy}"
password="${passwordDeploy}" trust="yes"
command="some command" />
<scp remoteTodir="${userDeploy}#${serverDeploy}:${destDir}"
password="${passwordDeploy}" trust="yes" sftp="true">
<fileset dir="${deployDir}" includes="*.jar" />
</scp>
<sshexec host="${serverDeploy}" username="${userDeploy}"
password="${passwordDeploy}" trust="yes"
command="some command" />
</target>
</configuration>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The properties are defined in profiles to allow for deployment in different servers (I know it's not the best possible approach, but this is the way things are done here), like this:
<profile>
<id>aprofile</id>
<properties>
<property name="serverDeploy" value="somevalue" />
<property name="userDeploy" value="someuser" />
<property name="passwordDeploy" value="somepassword" />
<!-- and so on -->
</properties>
</profile>
My problem is that I can't get maven properties to work in ant plugin; I tried to add a <echoproperties> task in ant to see which properties I have and there is no trace of maven properties.
Is it possible to use maven defined properties or should I use another approach? Any suggestion is welcome.
Edit: I modified the script as per first answer, it still doesn't work
You can pass the properties by defining new Ant properties (using the property tag in your target within the configuration). So for example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-module</artifactId>
<name>test-module</name>
<version>SNAPSHOT</version>
<properties>
<my.custom.property>false</my.custom.property>
</properties>
<profiles>
<profile>
<id>customProfile</id>
<properties>
<my.custom.property>true</my.custom.property>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<property name="antProperty" value="${my.custom.property}"/>
<echo message="Custom Ant Property is: ${antProperty}"/>
<echoproperties />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I execute mvn compile on this pom the output is:
main:
[echo] Custom Ant Property is: false
[echoproperties] #Ant properties
[echoproperties] #Thu Aug 08 17:17:30 CEST 2013
[echoproperties] ant.project.name=maven-antrun-
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
[echoproperties] antProperty=false
and when the command is mvn -PcustomProfile compile then the output is:
main:
[echo] Custom Ant Property is: true
[echoproperties] #Ant properties
[echoproperties] #Thu Aug 08 17:18:30 CEST 2013
[echoproperties] ant.project.name=maven-antrun-
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
[echoproperties] antProperty=true
This works using maven 3.0.5.
Most properties are automatically passed along to ant, at least if you're running an inlined ant script. Some of the properties get renamed. I suggest running "mvn -X" and the antrun plugin prints a list of all the variable mappings into ant (things like basedir becomes project.basedir, etc.)
On the newer versions of maven you can just use:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.plugin.classpath" />
example:
<build>
....
<plugins>
....
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.plugin.classpath" />
<echo message="Project name from Maven: ${project.name}" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
....
</plugins>
....
</build>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>

Maven Jetty run JNDI configuration

My project structure looks like this:
parent POM
|-- app-core
|-- app-model
|-- app-services
`-- app-web
In my pom.xml for app-web:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<webDefaultXml>src/test/resources/webdefault.xml</webDefaultXml>
</configuration>
</plugin>
Hot deployment works properly and have no issues with mvn jetty:run
Question : app-core uses springs how can I configure this JNDI for Jetty maven plugin in this multi-module hierarchy which should be accessible in app-core module
Update :
Have added pom.xml like this
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
<jettyConfig>src/main/resources/jetty.xml</jettyConfig>
</configuration>
</plugin>
And my jetty.xml looks like this :
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="icatDB" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/testDB</Arg>
<Arg>
<New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
<Set name="serverName">localhost</Set>
<Set name="databaseName">test</Set>
<Set name="user">sa</Set>
<Set name="password"></Set>
</New>
</Arg>
</New>
</Configure>
After that when i run mvn jetty:run I get this error
Failure: Object is not of type class org.mortbay.jetty.webapp.WebAppContext
Ok this works for me :
I need to use jettyEnvXml instead of jettyConfig.
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
<jettyEnvXml>src/main/resources/jetty.xml</jettyEnvXml>
</configuration>
</plugin>
See this jetty documentation for the configuration of JNDI in Jetty
See this Maven Jetty documentation for the way how to add the configuration files to the build process

jetty-maven-plugin setting buffers sizes

Returning to jetty-maven-plugin I've trouble to set buffers size.
My use-case imply file upload (usual size is ~700Ko).
Because the upload is too big for jetty-maven-plugin default configuration I get Http response with error status code 413 (request too large)
I tryied using plugin configuration :
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven.version}</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
<requestHeaderSize>8192</requestHeaderSize>
<requestBufferSize>2097152</requestBufferSize>
</connector>
</connectors>
</configuration>
</plugin>
Then I tried to use jetty-maven-plugin with a jetty.xml file
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven.version}</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<jettyConfig>${basedir}/src/main/config/jetty/jetty.xml</jettyConfig>
</configuration>
</plugin>
The jetty.xml is below:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
<Set name="requestHeaderSize">8192</Set>
<Set name="requestBufferSize">2097152</Set>
</New>
</Arg>
</Call>
</Configure>
Nothing works.
Could someone hand me the correct configuration please?
I'm not sure whether this fixes the problem in your use case, but you could try adding the following to your <configuration> section of the maven-jetty-plugin:
<systemProperties>
<systemProperty>
<name>org.eclipse.jetty.server.Request.maxFormContentSize</name>
<value>-1</value> <!-- or any other value -1 is for max -->
</systemProperty>
<systemProperties>
as mentioned by jesse mcconnell the property was renamed in jetty 7/8 to org.eclipse.jetty.server.Request.maxFormContentSize.
For jetty 6 for me org.mortbay.jetty.Request.maxFormContentSize is working.
change your pom as per this and add this two xml file into your project. I hope, it will work for you.
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<contextPath>/random-api</contextPath>
<scanIntervalSeconds>5</scanIntervalSeconds>
<jettyXml>jetty.xml,jetty-http.xml</jettyXml>
</configuration>
</plugin>
===============jetty.xml and jetty-http.xml=================
https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-tools/xwiki-platform-tool-jetty/xwiki-platform-tool-jetty-resources/src/main/resources/jetty/etc/jetty.xml
https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-tools/xwiki-platform-tool-jetty/xwiki-platform-tool-jetty-resources/src/main/resources/jetty/etc/jetty-http.xml
After a pause (lunch) I grabed the code of the web-app I was supposed to test.
There was a redundant limitation into its "internal" configuration (use an upload agent with it's own size limit).
In fact the two configuration proposed for jetty are working (now that web-app doesn't have any redundant limitation)

Resources