Jena schemagen in maven processing multiple ontologies - java

I am working on a project which uses multiple ontologies defined over several files. I am hoping to use Jena to generate the java classes to help with development but I can't seem to find a way to have Jena process multiple files as a maven goal.
I have never used Jena through maven before but I have used it on the command line (never for multiple ontologies at the same time).
The relevant part of my pom.xml is listed below (this was largely copied from the Jena website):
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>jena.schemagen</mainClass>
<commandlineArgs>
--inference \
-i ${basedir}/src/main/resources/ontologies/exception.owl \
--package com.borwell.dstl.mexs.ontology \
-o ${project.build.directory}/generated-sources/java \
</commandlineArgs>
<commandlineArgs>
--inference \
-i ${basedir}/src/main/resources/ontologies/location.owl \
--package com.borwell.dstl.mexs.ontology \
-o ${project.build.directory}/generated-sources/java \
</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I have had a good look around on the Jena website and other sites (my googlefoo is usually quite good) but I have been unable to find anyone else having this problem or any documentation explaining how to do this.
Any help on this would be very useful.

I wrote a custom Maven task for Jena Schemagen, which is now part of the standard Jena distribution. See the documentation here

EDIT
This Answer now exists and calls out a more standard solution. Namely, the existence of a schemagen-maven plugin that the poster had developed which is included in the standard Jena distribution.
Original Answer
You have two options, create a custom maven plugin (which is actually not that difficult) or shell out to something outside maven to handle your needs. I made a hack in the past which I'll share with you. It's not pretty, but it works well.
This approach uses Maven Ant Tasks to call out to an ant build.xml from within the generate-sources phase of a maven build.
You begin by modifying your pom.xml to including the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.runtime.classpath" />
<property name="runtime-classpath" refid="maven.runtime.classpath" />
<ant antfile="${basedir}/src/main/ant/build.xml"
inheritRefs="true">
<target name="my.generate-sources" />
</ant>
</target>
</configuration>
</execution>
</executions>
<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>
</dependencies>
</plugin>
</plugins>
</build>
In the build.xml file (which, as you can see, is located in src/main/ant/build.xml, is the following.
<?xml version="1.0" encoding = "UTF-8"?>
<!DOCTYPE project>
<project name="my" default="my.error">
<property name="vocab.out.root" location="${basedir}/src/main/java" />
<property name="vocab.package" value = "put something here" />
<property name="vocab.ns" value="put something here" />
<path id="my.vocabulary">
<fileset dir="${basedir}/src/main/resources/put something here" casesensitive="yes" >
<include name="**/*.owl" />
</fileset>
</path>
<scriptdef language="javascript" name="make-proper">
<attribute name="string" />
<attribute name="to" />
<![CDATA[
var raw_string = attributes.get( "string" );
var string_elements = raw_string.split('-');
var nameComponents;
var finalName = "";
var i;
for(i = 0; i < string_elements.length; i++){
var element = string_elements[i];
finalName += element.substr(0,1).toUpperCase() + element.substr(1);
}
project.setProperty( attributes.get( "to" ), finalName );
]]>
</scriptdef>
<!-- target for processing a file -->
<target name="my.schemagen-file">
<echo message="${vocab.file}" />
<!-- for constructing vocab file name -->
<local name="source.file.dir" />
<dirname property="source.file.dir" file="${vocab.file}" />
<local name="source.file" />
<basename property="source.file" file="${vocab.file}" suffix=".owl" />
<local name="vocab.name" />
<make-proper string="${source.file}" to="vocab.name" />
<!-- for constructing destination file name -->
<local name="vocab.package.path" />
<propertyregex property="vocab.package.path" input="${vocab.package}" regexp="\." replace="/" global="true" />
<local name="dest.file" />
<property name="dest.file" value="${vocab.out.root}/${vocab.package.path}/${vocab.name}.java" />
<!-- Determine if we should build, then build -->
<outofdate>
<sourcefiles path="${vocab.file}" />
<targetfiles path="${dest.file}" />
<sequential>
<!-- Actual construction of the destination file -->
<echo message="--inference --ontology -i ${vocab.file} -a ${vocab.ns} --package ${vocab.package} -o ${vocab.out.root} -n ${vocab.name}" />
<java classname="jena.schemagen" classpath="${runtime-classpath}">
<arg line="--ontology --nostrict -i ${vocab.file} -a ${vocab.ns} --package ${vocab.package} -o ${vocab.out.root} -n ${vocab.name}" />
</java>
</sequential>
</outofdate>
</target>
<!-- Maven antrun target to generate sources -->
<target name="my.generate-sources">
<foreach target="my.schemagen-file" param="vocab.file" inheritall="true">
<path refid="my.vocabulary"/>
</foreach>
</target>
<target name="my.error">
<fail message="This is not intended to be executed from the command line. Execute generate-sources goal using maven; ex:\nmvn generate-sources" />
</target>
</project>
Walking you through the whole thing.
The maven-antrun-plugin executes our build script , making sure that antcontrib are available as well as the maven.runtime.classpath. This ensures that properties that would be available within maven are available within the ant task as well.
If build.xml is called without specifying a task, it intentionally fails. This helps to keep you from using it wrong.
If the my.generate-sources target is executed, then we can use foreach from antcontrib in order to process each file within your specified directory. Adjust this file pattern however you need to. Note that an assumed extension of .owl is used elsewhere.
For each owl file, the my.schemagen-file target is called. I utilize local variables in order make this function.
A custom ant task (defined in javascript, named make-proper) helps to generate the destination file names for your files. I use a convention where my vocabulary files are in some-hyphenated-lowercase-structure.owl. My desired class name for that would be SomeHyphenatedLowercaseStructure.java. You should customize this however you see fit.
For each derived file name, we test to see if it exists and if it is out of date. This convenience only allows schemagen to run if the source file is newer than the destination file. We use the outofdate task to do this.
We run schemagen. I use flags specific to OWL vocabularies. You can adjust this however best suits your system.

After more generalised searching and thanks to DB5 I have found the solution to be multiple executions. I was hoping for a more elegant solution but this does work.
Credit to DB5

Related

Run testng suite just using jar or war file

I have created a testng test using selenium webdriver and java. Now I don't want to share my code to different users but I want my code to run by different users using jar or war file.
Can anyone help me out with this. Is it possible to run test without sharing testNG java code?
I haven't got an opportunity to work with maven but it should also be possible with maven
Let me tell you the idea by taking an example using ant
Assume you are running your tests using ant with the below build.xml(sample file change according to your needs) file
<project name="TestNG Demo" default="clean" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="${ws.home}/Jars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="ng.result" value="test-output"/>
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="**/*.jar"/>
<pathelement path="${ws.jars}"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath------: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
includeantruntime="false"
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.6"
classpath="${test.classpath}"
>
</javac>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<!-- run -->
<target name="run" >
<testng classpath="${test.classpath}:${test.dest}" suitename="suite">
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="makexsltreports">
<mkdir dir="${ws.home}/XSLT_Reports/output"/>
<xslt in="${ng.result}/testng-results.xml" style="src/demo/testng-results.xsl"
out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
<param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
</xslt>
</target>
<!-- ****************** targets not used ****************** -->
</project>
Now after test compiles(use target ant compile to compile your tests) you will get the class files in build folder inside your project folder and now you can delete the src folder(your java files) and to use ant run to execute the tests(use target ant run) to run your tests.If your planning to give it your client then you can make a simple bat(windows) or shellscript(linux) to execute command ant run and on clicking on it the test will run
Hope it helps you..Kindly get back if you have any queries
As #vicky suggests, look at Maven, as it'll allow you to package up your project with both a production WAR/JAR and also the tests packaged up into a test JAR. Specifically at the test-jar JAR type you can label JARs with.
So add this as a plugin to the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then others can pull it as a dependency using:
<dependency>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
<type>test-jar</type>
<scope>test</scope>
A bit more information here: How do I install a test-jar in maven?

Google API Client Library in Java set-up error in Eclipse

I am trying to download the source-code of Google API Client Library written in Java and perform some local developments. However, setting up the project seems to be a big hurdle.
I meticulously followed the instructions written here:
https://developers.google.com/api-client-library/java/google-api-java-client/contribute
However, I am still getting a bunch of error, possibly due to maven settings. I have tried updating the projects and downloading sources and Javadoc. Still, I am getting these errors. Spend nearly 3-4 hours.
The main errors are of these kinds:
1.Missing artifact com.google.api-client:google-api-client-servlet:jar:1.21.0-SNAPSHOT
Error Point:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-servlet</artifactId>
</dependency>
2.Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-antrun-plugin:1.6:run (execution: generate-sources, phase: generate-sources)
Error Point:
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<target>
<mkdir dir="target/generated-test-sources" />
<exec failonerror="true" executable="protoc">
<arg value="--java_out=target/generated-test-sources" />
<arg value="src/test/java/com/google/api/client/googleapis/batch/mock_data.proto" />
<arg value="src/test/java/com/google/api/client/googleapis/batch/error_output.proto" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
3.The container 'Maven Dependencies' references non existing library '/home/sohamgrad/.m2/repository/com/google/oauth-client/google-oauth-client-appengine/1.21.0-SNAPSHOT/google-oauth-client-appengine-1.21.0-SNAPSHOT.jar'
No error point in this case.
These are the main 3 types of errors. These types of errors are happening in multiple places.
I would appreciate any kind of help in this.

Maven concat files specific files in a directory

I have a directory with unknown depth and folder names.
>A
-> AB
--> configuration.xml
--> ABC
---> configuration.xml
-> AD
--> configuration.xml
-> allconfigurations.xml
I need a maven plugin to concat all the configuration.xml files and create allconfigurations.xml file in the root. Unfortunately folder names and depth is unknown. It would be great to accomplish it within the pom.xml file without needing any other files.
just a quick google for you: the maven-shade-plugin with the XmlAppendingTransformer could help.
sample config is here
While struggling I realized that the real problem is to put a header and footer for the compiled allconfigurations.xml file, since each configuration.xml file is a fragment and when I concat all of them together resulting xml is not a valid xml.
here is the case;
an xml file is something like:
<Configuration xmlns="abc">
...
<connectionTimeoutInMs>240000</connectionTimeoutInMs>
<socketTimeoutInMs>240000</socketTimeoutInMs>
<persist>false</persist>
<internal>false</internal>
...
</Configuration>
and putting many of them is not valid thus result xml must be something like;
<AllConfigurations xmlns="abc">
...
<Configuration xmlns="abc">
...
</Configuration >
...
<AllConfigurations xmlns="abc">
so the first and last lines must be added to the result.
here is the solution I came up with;
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>default-cli</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<!--<phase>process-resources</phase>-->
<!--<phase>compile</phase>-->
<configuration>
<target>
<concat destfile="${project.basedir}/.../allConfigurations.xml"
force="yes">
<fileset dir="${project.basedir}/...">
<include name="xmlHeaderForConfiguration"></include>
</fileset>
<fileset dir="${project.basedir}/...">
<include name="**/configuration.xml"></include>
</fileset>
<fileset dir="${project.basedir}/...">
<include name="xmlFooterForConfiguration"></include>
</fileset>
</concat>
</target>
</configuration>
</execution>
</executions>
</plugin>
where xmlHeaderForConfiguration is a file with content; <AllConfigurations xmlns="abc"> and xmlHeaderForConfiguration has </AllConfigurations>

Ant task to do a Maven build - but use a different version of Java than the rest of the Ant script

Hi,
I am trying to include a Maven build into my Ant build. I have this going fine when I use just the Ant task involved, using the Maven Ant plugin.
My problem is that my Maven code includes a 1.6 dependent .jar file, but my Ant script is 1.5 dependent so will fail if run with 1.6. So I have to create a process which switches JVM to run my Maven target in my Ant script.
I have tried:
Adding a plugin to the build section of my master POM which customizes the compiler:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<executable>"c:\Program Files (x86)\Java\jdk1.6.0_45\bin\javac.exe"</executable>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
This does not work as it says it has a problem with -s. I've checked and it is a Java 1.6 parameter to javac which does not exist in 1.5! So it is creating the javac call for 1.6 and then running it with the 1.5 javac. Also, I would prefer to only be editing the Ant scripts to do this.
I have also tried to every combination of <property> and <sysproperty> (in the Java call) and <arg> to set the PATH and JAVA_HOME variables but they don't seem to work the way I need them to. Is it that this is just not possible? I even set the JVM attribute of the <java> tag to run against the 1.6 java but it still seems to have issues with the javac the Maven is calling to do the build.
Thanks in advance.
Here's the section from the Maven site (polished slightly) for convenience:
<macrodef name="maven">
<attribute name="options" default="" />
<attribute name="goal" />
<attribute name="basedir" />
<attribute name="resultproperty" default="maven.result" />
<element name="args" implicit="true" optional="true" />
<sequential>
<java classname="org.codehaus.classworlds.Launcher" fork="true" dir="#{basedir}" resultproperty="#{resultproperty}">
<jvmarg value="-Xmx512m"/>
<classpath>
<fileset dir="${maven.home}/boot"><include name="*.jar" /></fileset>
<fileset dir="${maven.home}/lib"><include name="*.jar" /></fileset>
</classpath>
<sysproperty key="classworlds.conf" value="${maven.home}/bin/m2.conf" />
<sysproperty key="maven.home" value="${maven.home}" />
<arg line="--batch-mode #{options} #{goal}" />
</java>
</sequential>
</macrodef>
<target name="my_maven_target">
<maven basedir="${basedir}" options="${maven.opts}" goal="install" resultproperty="maven.build.result"/>
</target>
You should try using the ant exec Task for this. Look here for a previous detailed answer to this: Run ant task in different jvm

Executable war file that starts jetty without maven

I'm trying to make an "executable" war file (java -jar myWarFile.war) that will start up a Jetty webserver that hosts the webapp contained in the WAR file I executed.
I found a page that described how to make what I'm looking for:
However, following that advice along with how I think I'm supposed to make an executable jar (war) isn't working.
I have an Ant task creating a WAR file with a manifest that looks like:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_18-b02 (Sun Microsystems Inc.)
Main-Class: Start
The contents of the WAR file look like:
> Start.class
> jsp
> build.jsp
> META-INF
> MANIFEST.MF
> WEB-INF
> lib
> jetty-6.1.22.jar
> jetty-util.6.1.22.jar
When I try to execute the WAR file, the error is:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Start. Program will exit.
There appears to be two errors here: one where it seems the JAR files can't be found, and one where the Start class can't be found.
To fix the first one, I put the Jetty JAR files in the base of the WAR file and tried again -- same error. I also tried adding the WEB-INF/lib/<specific-JAR-files> to the Class-Path attribute of the manifest. That did not work either.
Does anyone have any insight as to what I'm doing right/wrong and how I can get this executable WAR file up and running?
The link you have in your question provides most of what you need. However, there are a few things that need to be done in addition to that.
Any class files that Jetty needs to start up will need to be located at the root of the war file when it's packaged. We can leverage Ant to do that for us before we <war> the file. The war's manifest file will also need a Main-Class attribute to execute the server.
Here's a step-by-step:
Create your Jetty server class:
This is adapted from the link you provided.
package com.mycompany.myapp;
import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
public final class EmbeddedJettyServer
{
public static void main(String[] args) throws Exception
{
int port = Integer.parseInt(System.getProperty("port", "8080"));
Server server = new Server(port);
ProtectionDomain domain = EmbeddedJettyServer.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
webapp.setServer(server);
webapp.setWar(location.toExternalForm());
// (Optional) Set the directory the war will extract to.
// If not set, java.io.tmpdir will be used, which can cause problems
// if the temp directory gets cleaned periodically.
// Your build scripts should remove this directory between deployments
webapp.setTempDirectory(new File("/path/to/webapp-directory"));
server.setHandler(webapp);
server.start();
server.join();
}
}
To see what all you can configure here, have a look at the Jetty API documentation.
Build the war with Ant:
This uses a staging directory to unpack the necessary class files into the root of the war so they're accessible when the war is executed.
<target name="war" description="--> Creates self-executing war">
<property name="staging.dir" location="${basedir}/staging"/>
<property name="webapp.dir" location="${basedir}/src/webapp"/>
<mkdir dir="${staging.dir}"/>
<!-- assumes you have all of your war content (excluding classes and libraries) already structured in a directory called src/webapp -->
<!-- e.g. -->
<!-- src/webapp/index.html -->
<!-- src/webapp/WEB-INF/web.xml -->
<!-- src/webapp/WEB-INF/classes/my.properties -->
<!-- etc ... -->
<copy todir="${staging.dir}">
<fileset dir="${webapp.dir}" includes="**/*"/>
</copy>
<unjar dest="${staging.dir}">
<!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
<!-- you might find some of them in the downloaded Jetty .tgz -->
<fileset dir="path/to/jetty/jars">
<include name="ant-1.6.5.jar"/>
<include name="core-3.1.1.jar"/>
<include name="jetty-6.1.24.jar"/>
<include name="jsp-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
<include name="jsp-api-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
<include name="servlet-api-2.5-20081211.jar"/><!-- your Servlet API implementation may vary -->
</fileset>
<patternset><!-- to exclude some of the stuff we don't really need -->
<exclude name="META-INF/**/*"/>
<exclude name="images/**/*"/>
<exclude name=".options"/>
<exclude name="about.html"/>
<exclude name="jdtCompilerAdapter.jar"/>
<exclude name="plugin*"/>
</patternset>
</unjar>
<!-- copy in the class file built from the above EmbeddedJettyServer.java -->
<copy todir="${staging.dir}">
<fileset dir="path/to/classes/dir" includes="com/mycompany/myapp/EmbeddedJettyServer.class"/>
</copy>
<war destfile="myapp.war" webxml="${webapp.dir}/WEB-INF/web.xml">
<fileset dir="${staging.dir}" includes="**/*"/>
<classes dir="path/to/classes/dir"/><!-- your application classes -->
<lib dir="path/to/lib/dir"/><!-- application dependency jars -->
<manifest>
<!-- add the Main-Class attribute that will execute our server class -->
<attribute name="Main-Class" value="com.mycompany.myapp.EmbeddedJettyServer"/>
</manifest>
</war>
<delete dir="${staging.dir}"/>
</target>
Execute the war:
If everything's set up properly above, you should be able to:
java -jar myapp.war
// or if you want to configure the port (since we are using the System property in the code)
java -Dport=8443 -jar myapp.war
This is an adaptation for Maven of #RobHruska's answer. It just copies the files of the main class and merges the Jetty JAR files into the WAR file, nothing new, just to simplify your life if you are new -like me- to Maven:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>move-main-class</id>
<phase>compile</phase>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${project.build.finalName}">
<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/">
<include name="main/*.class" />
</fileset>
</copy>
<unjar dest="${project.build.directory}/${project.build.finalName}">
<!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
<!-- you might find some of them in the downloaded Jetty .tgz -->
<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/lib/">
<include name="ant-1.6.5.jar"/>
<!--<include name="core-3.1.1.jar"/>-->
<include name="jetty*"/>
<include name="servlet-api*"/>
</fileset>
<patternset><!-- to exclude some of the stuff we don't really need -->
<exclude name="META-INF/**/*"/>
<exclude name="images/**/*"/>
<exclude name=".options"/>
<exclude name="about.html"/>
<exclude name="jdtCompilerAdapter.jar"/>
<exclude name="plugin*"/>
</patternset>
</unjar>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archiveClasses>true</archiveClasses>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
We have figured this out by using jetty-console-maven-plugin.
Whenever you run mvn package it creates another war that can be used with java -jar whateverpackage-runnable.war
<plugin>
<groupId>org.simplericity.jettyconsole</groupId>
<artifactId>jetty-console-maven-plugin</artifactId>
<version>1.45</version>
<executions>
<execution>
<goals>
<goal>createconsole</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalDependencies>
<additionalDependency>
<artifactId>jetty-console-requestlog-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-gzip-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-ajp-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-startstop-plugin</artifactId>
</additionalDependency>
</additionalDependencies>
</configuration>
</plugin>
It also generates the init.d scripts and everything for you!
Hudson solves this exact problem using the Winstone servlet container, which supports this use case directly. http://winstone.sourceforge.net/#embedding
Perhaps this would work for you?
Even though this is kind of old another alternative with Jetty 8 is to simply include the Jetty jars as dependencies in your pom and add the following in your pom (versus an ant script that unpackages the war and repackages it):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>JettyStandaloneMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- The main class needs to be in the root of the war in order to be
runnable -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>move-main-class</id>
<phase>compile</phase>
<configuration>
<tasks>
<move todir="${project.build.directory}/${project.build.finalName}">
<fileset dir="${project.build.directory}/classes/">
<include name="JettyStandaloneMain.class" />
</fileset>
</move>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I take it that by "without maven" you want a jar that you can run by itself and not with "mvn jetty:run"--not that you don't want to use maven at all.
It took me way to long to figure this out because I found many options--none of them dirt simple. Eventually I found this maven plugin from simplericity. It works wonderfully.
This is my example ANT extract. The idea is to unpackage the Jetty dependencies and then include them locally just like a normal JAR file:
<!-- Hack: Java doesn't support jars within jars/wars -->
<unjar src="${lib.dir}/container/jetty.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jetty-util.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/servlet-api.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jsp-api.jar" dest="${build.dir}/unjar"/>
<!-- Build war file as normal, just including the compiled and unjar'ed files -->
<war destfile="${war.file}" webxml="${config.dir}/web.xml">
<fileset dir="${build.dir}/classes"/>
<fileset dir="${build.dir}/unjar"/>
<fileset dir="${resources.dir}" excludes="*.swp"/>
<lib dir="${lib.dir}/runtime"/>
<manifest>
<attribute name="Main-Class" value="Start"/>
</manifest>
</war>
Note:
The WEB-INF/lib direcory is for the web applications dependencies. In this case we're packaging the WAR file so that it works like the normal Jetty JAR file on startup
Putting .jars inside a .war file root does nothing
Putting .jars inside WEB-INF/lib doesn't help the JVM find the Jetty files to even begin launching your .war. It's "too late" to put them there.
Putting .jars in the manifest Class-Path only works for external .jar files, not those contained in the .jar
So what to do?
Use a build script to simply merge all the .jar files you need into the .war file. This takes a little extra work. It's also a bit ugly in that the compiled code is part of the servable files in the .war
Add dependent .jars to the JVM's classpath with "java -cp jetty.jar:... ..." Works though this kind of defeats the purpose of one stand-alone .war
I have done a similar thing before but are you launchign the app as "java -jar xxx.war" ?. You have only 2 jars and it is not going to be enough I think. Also try using Jetty 7.0.0M1 (which is the latest version). When I added jetty-server and jetty-webapp as two dependencies (they are from org.eclipse.jetty) I get the following jar's in the lib directory. FYI the org.mortbay.jetty.Handler was in the jetty-server*.jar.
jetty-continuation-7.0.0.M1.jar
jetty-http-7.0.0.M1.jar
jetty-io-7.0.0.M1.jar
jetty-security-7.0.0.M1.jar
jetty-server-7.0.0.M1.jar
jetty-servlet-7.0.0.M1.jar
jetty-util-7.0.0.M1.jar
jetty-webapp-7.0.0.M1.jar
jetty-xml-7.0.0.M1.jar

Categories