I'm starting with Ant. I created a build.xml to generate a WAR file of a Web Project and it worked OK.
Then, I made some change to exclude all *.jar from WEB-INF/lib Folder and also works OK.
Now I need to make the changes to exclude all JARs files, but leave some especial JARs in WEB-INF/lib Folder. This JARs are from other project created by me.
The idea es exclude all third parties JARs and only leave my own JARs inside WEB-INF/lib folder.
There is some way to do that?
All my Jars start with "fnet" so maybe I can use that to create some rule, but I don't know how to do that
This is my Build.xml:
<?xml version="1.0" ?>
<project name="warConLibs" default="build-war">
<target name="clean">
<delete file="c:/projweb.war"/>
<delete file="c:/projweb_sl.war"/>
</target>
<target name="build-war">
<war destfile="c:/projweb.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
<target name="build-war-sin-libs">
<war destfile="c:/projweb_sl.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
<exclude name="**/*.jar"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
</project>
The correct way to exclude a jar file is given in the documentation. If anyone face same issue, they can refer to this link.
This example is taken from the documentation, here we are removing jdbc1.jar from lib
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
You may want to read again about the war Ant task: https://ant.apache.org/manual/Tasks/war.html
The correct syntax would be:
<war destfile="..." webxml="...">
<lib dir="WebContent/WEB-INF/lib">
<include name="fnet*.jar"/>
</lib>
<classes dir="bin"/>
</war>
Related
I have some problem, in my friend's code there is a clean code using apache ant seems to delete lib folder as well. but when I try to create project, it fails because the lib folder is missing. then how to build the correctly using ant apache ?
this is clean code
<target name="clean" description="--> clean the project">
<delete includeemptydirs="true">
<fileset dir="${basedir}">
<exclude name="src/**" />
<exclude name="build.xml" />
<exclude name="ivy.xml" />
</fileset>
</delete>
</target>
and this build code
<target name="build" description="Compile main source tree java files">
<mkdir dir="${build.dir}" />
<javac srcdir="${java.dir}" destdir="${build.dir}" classpathref="lib.path.id" debug="true" deprecation="true" optimize="true" failonerror="true" />
<!-- class path properties files -->
<copy file="${resource.dir}/log4j.properties" todir="${build.dir}" />
<copy file="${resource.dir}/mncplaymedia.properties" todir="${build.dir}" />
<copy todir="${build.dir}">
<fileset dir="${resource.dir}" />
</copy>
</target>
thank you
I think you have to build this project, then put the war or jar file in webapps on your web server (like jetty, tomcat and etc.)
I have a build.xml and I have a path for the classpath that I set to classpathref="compile.classpath" during compile:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
</path>
The lib folder contains weblogic.jar but when i try to compile the project, i got many errors because of missing the weblogic.jar
If I modify my path to this:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
<fileset dir="${env.WL_HOME}/wlserver/server/lib">
<include name="weblogic.jar" />
</fileset>
</path>
So I add the weblogic.jar from my local installed weblogic directory, there are no errors, and it's compiled.
I copied the weblogic.jar to my project lib folder from the local installed weblogic folder, so it must be the same weblogic.jar
What should I try? Thank you!
I would do something like this in your build.xml (probably just before you do the compilation will work).
<property name="echo.classpath" refid="compile.classpath"/>
<echo message="compileClasspath - ${echo.classpath}"/>
What you probably need to do is to be quite explicit about where your lib directory is, relative paths are tricky if you have multiple build.xml files, and nested directories and stuff.
What I have done before is to make sure that you explicitly define a property in the right place for your lib directory, and just use that rather than ./
<project basedir=".">
<target name="init">
<property name="local.lib.dir" value="${basedir}/lib">
</target>
<target name="compile" depends="init">
<path id="compile.classpath">
<fileset dir="${local.lib.dir}">
<include name="*" />
</fileset>
</path>
....
</target>
</project>
i'm generating a war with an Ant script but i noticed that the Ant's war size is almost double the size of the war created by Eclipse. And now i'm want to know if it's possible to reduce this size.
This is the war script:
<war destfile="${docflow4-web-home}/deploy/docflow.war" webxml="${docflow4-web-home}/web/WEB-INF/web.xml">
<classes dir="${docflow4-web-home}/web/WEB-INF/classes" />
<lib dir="${docflow4-web-home}/web/WEB-INF/lib"/>
<fileset dir="${docflow4-web-home}/web">
<include name="**/*.*"/>
</fileset>
</war>
My answer to my problem was to remove the "class" and the "lib" tag from the script, they were duplicating the files
<war destfile="${docflow4-web-home}/deploy/docflow.war" webxml="${docflow4-web-home}/web/WEB-INF/web.xml">
<fileset dir="${docflow4-web-home}/web">
<include name="**/*.*"/>
</fileset>
</war>
I am making a war of a project but i want that classes within that war be picked from specified location and also a jar file A.jar to be picked from custom location i am doing something like this ....
<target name="war" depends="a-jar">
<war destfile="D:/JBOSSHOME/project.war" webxml="${project-location}/web/WEB-INF/web.xml">
<fileset dir="${project-location}/web" >
<exclude name="${project-location}/web/WEB-INF/lib/A.jar"/>
<exclude name="${project-location}/web/WEB-INF/classes/**/*.class"/>
</fileset>
<lib dir="${jarsLocation}"></lib>
<classes dir="D:/JBOSSHOME/project/build/classes"/>
</war>
</target>
but in the resultant war i am getting 2 A.jar files and also classes are getting copied from ${project-location}/web/WEB-INF/classes instead of the classes dir that i have provided...
Any help would be much appreciated...
The exclude shouldn't include the root directory of your file set:
<fileset dir="${project-location}/web" >
<exclude name="WEB-INF/lib/A.jar"/>
<exclude name="WEB-INF/classes/**/*.class"/>
</fileset>
I have the following directory structure of a project:
Folder "project" in Eclipse:
--folder "src"
--folder "resources"
----trayicon2.png
--folder "db"
----test.db
--folder "bin"
I'm accessing the image with:
Image image = Toolkit.getDefaultToolkit().getImage("resources/trayicon2.png");
and from Eclipse that is not a problem.
Then I generate an "executable jar file", and add the dirs, making a directory structure of:
Folder "project"
--folder "db"
----test.db
--folder "resources"
----trayicon2.png
--project.jar
And now the image is no more accessible. Also, the database is no more accessible; while in Eclipse I used to access it with:
Connection conn = DriverManager.getConnection("jdbc:sqlite:db/test.db");
How can I access the resources (images and db) after generating the jar file "project.jar"?
For the image, try this:
URL imageURL = getClass().getResource("/resource/trayicon2.png"); // note leading slash
Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
When you are in eclipse the filename you pass is relative and it is resolved properly with the classpath that is automatically setup by eclipse. Try using the URL version of the method and pass the URL to your jar file. You can see an example on this page
You may want to switch to ANT to build your jar files.
You can have the image resources built into the jar where they can be found, and any required libraries (like those for sqlite, put into a lib folder inside the jar as well).
Here's a sample build file:
<?xml version="1.0"?>
<project name="test" default="compile" basedir=".">
<description>
Generates jar for project
</description>
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.classes" value="${build.dir}/classes"/>
<property name="build.jars" value="${build.dir}/jars"/>
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.jars}"/>
</target>
<path id="core.classpath">
<fileset dir="${build.jars}">
<include name="**/*.jar"/>
</fileset>
</path>
<path id='lib.classpath'>
<fileset dir='lib' />
</path>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.classes}" debug="true">
<classpath refid="lib.classpath"/>
</javac>
</target>
<target name="copy-resources">
<copy todir="${build.classes}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="jar" depends="compile, copy-resources">
<jar destfile="${build.jars}/project.jar">
<fileset dir="${build.classes}">
<include name="**/*.class"/>
<include name="**/*.properties"/>
<include name="**/*.png"/>
</fileset>
<fileset dir=".">
<include name="**/*.jar"/>
</fileset>
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="com.your.path.to.MainClass"/>
</manifest>
</jar>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${build.classes}"/>
<delete dir="${build.jars}"/>
</target>
</project>
This assumes you have some java files in a src directory, and some libraries in a lib folder. So your path would look like this:
Project
----------
-bin
-src
--com...
---MainClass (has a main method, runs your program)
---a.png (will be findable)
-lib
--jar files required by your project
build.xml
-----------
It will also copy any properties or png files you have inside the src folders.
You would then type ANT jar at the level of build.xml to build the jar file.
You can't access resources inside a JAR file directly. Instead, you need to use the Class's URL getResource(String) (or InputStream getReourceAsStream(String)) methods.
I'm not really sure how you'd use that with the SQLite database, though.