ant: include resources in subdirectory to war file - java

I would like to include a set of resource files to my war file.
I know that I can specify either fileset or webinf as an inner element of my war element in build.xml, but in both of these cases, when I deploy the war file, the resource files end up in either top level directory or WEB-INF directory respectively.
I would like to place them into a subdirectory (say, WEB-INF/resources). Actually, these resource files are in a subdirectory in my source three already, I just need to include this subdirectory as is to the war file.
How is this done?

You can use zipfileset with prefix attribute to add a subdirectory to zip/war file. If the resource files is in src/resource, and you want to pack them into WEB-INF/resource. Here is the example:
<target name="build">
<war destfile="test.war">
<fileset dir="webapps">
<include name="**" />
</fileset>
<zipfileset dir="src/resource" prefix="WEB-INF/resource" />
</war>
</target>

Related

Exclude a file /directory in fileset tag before executing a target

I am writing an Ant buildfile for a project structured like this:
build.xml
| child_build1.xml
| child_build2.xml
| calling generated jar file from child_build_1.xml
Calling Ant using the command
ant -f build clean build-project
here clean and build-project are targets.
child_build2.xml generates a jar file which includes some other jar files with child_build1.jar file.
I am using a temp folder to place jar files generated by build and generating an rpm.
To include internal jars I am using a fileset to include jars.
<path id="classpath.migrate">
<path refid="classpath"/>
<fileset dir="${parent}/test">
<include name="test1.jar"/>
</fileset>
<fileset dir="${parent}/bin">
<include name="xxx.jar"/>
<include name="yyyy.jar"/>
</fileset>
</path>
<target name="build-project">
<mkdir dir="${parent}/bin/" />
<mkdir dir="${parent}/lib/" />
...creating of parent directory and jar creation logic
</target>
<target name="clean">
...clearing all the file
</target>
I have declared values in a build.properties file which I have included in property tag.
While triggering the "clean" target Ant tries to parse, but fails to find ${parent} folder. Actually the parent folder ${parent} will be generated in "build" target.
I am generating the rpm using an rpmbuild command from a script. Script intern calls rpmbuild with spec file. Ant script is called in spec file.
build.properties:
root.dir = rootdirectory
test.dir = ${root.dir}/test/
dist.dir = ${build.dir}/dist/
In spec Ant file is called with parent directory value. So there is no hard coded value.
Is there any way to exclude the checking mechanism and directly exeucte the target or skip the errors in fileset?
There is an attribute erroronmissingdir in fileset to skip the error in clear target.

How to include a property file in WEB-INF folder of war

I am using ant to build my web-app. I am trying to include a property file in the WEB-INF folder from a source folder. I have included it in the war/WEB-APP/classes folder. But the application is not reading it. Hence, i want to include it in the WEB-INF folder directly to read it in the application.
I have tried the following but nothing seems to work. my build.xml looks like this :
<target name="build-dev" description="Package GWT app to web archive and deploy to web server">
<echo message="Package GWT app to web archive" />
<copy toDir="${basedir}/war/WEB-INF/lib">
<fileset dir="${basedir}/lib" includes="*.jar" />
<fileset dir="${gwt.home}" includes="*.jar" />
</copy>
<copy todir="${basedir}/war" file="${basedir}/src/etc/dev/GroupQuoteUI.properties" />
<war basedir="${war.dir}" destfile="${deploy.dir}/${app.name}.war" webxml="${webinf.dir}/web.xml">
<webinf dir="${webinf.dir}/">
<include name="*." />
<exclude name="**/web.xml" />
</webinf>
<classes dir="${basedir}/src/etc/dev/" includes="*.properties" />
</war>
</target>
i have tried to use :
"include name="${war.dir}/GroupQuoteUI.properties" in "webinf" tag but it did'nt worked.
Also includes="${war.dir}/GroupQuoteUI.properties" inside the tag.
Also this inside "webinf" folder again :
"zipfileset dir="${basedir}/war/" includes="GroupQuoteUI.properties" fullpath="${webinf.dir}/GroupQuoteUI.properties"
but this is giving an error during build stating "cannot have src dir together".
So what should i do to include this file in the WEB-INF directory of the war. All other directories and web.xml file is included.
You cannot read a file that is packed into a war, or jar by accessing it with
new FileInputStream()
Instead you can do the following:
this.getClass().getResourceAsStream(filename)
this will load a resource (your properties-file) from the classpath, so it will read the file in a war-archive.
It does this by using the same ClasseLoader which has been used to load the class that belongs to this.getClass()
Here you can find an example:
How to really read text file from classpath in Java

Maven: how to copy resources into WAR?

I have an Eclipse webapp project that I am trying to migrate from Ant build to Maven build. I have a lot of non-Java files in my src/ folder, for example log4j2.xml, ehcache.xml, some .properties localization files etc.
When I run war:war target on this project, the resulting WAR file contains only the .class files in the WEB-INF/classes folder, all the non-Java files are missing. In Ant I did this:
<target name="copy-resources" depends="compile">
<copy todir="${build}">
<fileset dir="${src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
How do I achieve the same thing in Maven? I suspect I should be using the resources folder, but right now I am trying to migrate with as little changes to the original codebase as possible...
You can access these files from resources folder. I hope this
link helps.
You can also specify the folder, if it is not the default resources folder, in the pom.xml. See
Specifying resource directories.
For example, if your folder has these files in src/my-resources, then you need to add this to the pom.xml, and then you would be able access it in code.
<resources>
<resource>
<directory>src/my-resources</directory>
</resource>
</resources>

Creating a WAR file using Java

How do I create a deployable WAR file for Apache Tomcat servers programmatically with Java?
Is there a library for such a task?
I am working on a small own IDE for special purposes. The IDE is written in Java and JavaScript, so I need to create the WAR file using those.
If you want to build it from code try to do it from the command line with
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("jar cvf /path/to/your/project/your-file.war");
Of course, the same thing would work using ANT or Maven (as long as those tools are installed on the final platform).
Edit: added improvement suggestion
I don't know of libraries, but a WAR file is just a ZIP file with a different ending.
Just create the inner folder structure and files (google the java code for that) and package as zip (java has methods for that too I think, again google) and rename the file from "myfile.zip" to "myfile.war"
I don't know how you would do it using the IDE you have. But a WAR file has the following structure:
web resources go to the root
project classes (including their package folders) go to a folder WEB-INF/classes
project dependency jars go to WEB-INF/lib
So if you want to build a WAR by hand, you need to create that file structure inside a zip file with a .war extension and you need to copy that to the proper location of the server to deploy it. Most servers also allow 'exploded deployment', meaning that you don't need an actual war file, you can just deploy the stuff to a directory with the same name as your war (IE. 'myapp.war').
You can do this a number of ways, for a quick example if you are using maven you just need to use <packaging>war</packaging>
You could just export the war as has been mentioned, but it's not exactly "programmatic".
If you're using Ant - you can find a tutorial for this here
<?xml version="1.0" ?>
<path id="compile.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="build/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="/APP/jboss-5.1.0.GA/server/all/deploy/DispatchActionEx.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent"/>
<lib dir="WebContent/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
<target name="clean">
</target>
Just write an build.xml file(i have give an example),
change "project name" and "war destfile" which will be ".../apache-tomcat/webapps/projectname.war"
put it in your project folder
open it in eclipse.
right click on it>>run as>>ant build
check whether the war file is created in the webapps folder in apache-tomcat

Create directory structures during Ant jar task

I want to create a jar with the following directory structure:
thejar.jar/
classes/ --> where all classes go
lib/ --> where all dependencies go
res/ --> where all non-classpath resources go (scripts, etc.)
META-INF/
Here's my ant task:
<jar destfile="dist/main/thejar.jar">
<!-- Create the manifest -->
<manifest>
<!-- JAR should be sealed. -->
<attribute name="Sealed" value="true" />
</manifest>
<!-- Copy main build directory to classes/ directory in JAR. -->
<fileset dir="dist/main/classes" includes="build/main"/>
<!-- Copy main library directory to lib/ directory in JAR. -->
<fileset dir="dist/main/lib" includes="lib/main"/>
<!-- Copy main resources directory to res/ in JAR. -->
<fileset dir="dist/main/res" includes="res/main"/>
</jar>
If I am understanding this correctly, it should be:
Copying all the built (.class) files in build/main to dist/main/classes
Copying all lib/main dependencies to dist/main/lib
Copying all res/main files to dist/main/res
JARring up dist/main/* into thejar.jar
The JAR task executes without errors, but when I go to view the contents of thejar.jar I just see META-INF/ (none of the subdirectories I mentioned above).
What's going on here? Thanks in advance!
You want jar task to copy files in build/main into jar's dist/main/lib, but
<fileset dir="dist/main/classes" includes="build/main"/>
means to pack files in dist/main/classes/build/main into the jar file.
Take a look at the example from the Ant-Jar task doc:
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class"
/>
<fileset dir="${src}/resources"/>
</jar>
(The code above) jars all files in the ${build}/classes directory and also in the ${src}/resources directory together into a file called app.jar in the ${dist}/lib directory.
To achieve your request, I think you can copy the classes, resources and dependencies with <copy> task into the directory structure that you want and then jar the directory.

Categories