Create a WAR file with version using ANT - java

I have a problem trying to build a WAR file with a version number.
This is my build.properties
jboss.home=D\:\java\jboss-as-7.1.1.Final
version=2.0
And this is part of my ANT task
<project name="AS2-jBoss7" basedir="../" default="deploy">
<!-- Configuraciones del proyecto -->
<property name="project.distname" value="AS2" />
<!-- Paths locales del sistema -->
<property file="${basedir}/ant/build.properties" />
<property name="webroot.dir" value="${basedir}/WebContent" />
<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
<property name="build.dir" value="build" />
<!-- Build project -->
<target name="build" depends="prepare,compile" />
<target name="rebuild" depends="clean,prepare,compile" />
<!-- Crea el war de la aplicacion -->
<target name="war" depends="build">
<mkdir dir="${build.dir}" />
<war basedir="${webroot.dir}" warfile="${build.dir}/${project.distname}_${version}.war" webxml="${webinf.dir}/web.xml">
<exclude name="WEB-INF/${build.dir}/**" />
<exclude name="WEB-INF/otros/**" />
</war>
</target>
<!-- Despliega el proyecto -->
<target name="deploy" depends="war">
<delete file="${deploy.dir}/${project.distname}.war" />
<delete dir="${deploy.dir}/${project.distname}" />
<copy file="${build.dir}/${project.distname}_${version}.war" todir="${deploy.dir}" />
</target>
</project>
When I build my WAR file the name is AS2_2.0.war and that is correct, but when I view the site in the web browser I have to write the url:
http://localhost:8030/AS2_2.0/login.jsf
What I want is that the WAR version is AS2_2.0 but the URL does not contain the version number, how could I do that?

This has nothing to do with ANT, your application server is using the name of the WAR by default. What you want to do is change the "context root" for your web application. If you Google that phrase, along with the name of your application server (e.g. "Tomcat" or "JBoss"), I'm sure you'll find your answer. Or, if you tell me what server you're using I can try and help you.

See http://ant.apache.org/manual/Tasks/copy.html In examples, you can copy a single file with ant just like this,
<copy file="myfile.txt" tofile="mycopy.txt"/>
In your case supposing you want the deployed file to be named AS try
<target name="deploy" depends="war">
<delete file="${deploy.dir}/${project.distname}/AS.war" />
<delete dir="${deploy.dir}/${project.distname}" />
<copy file="${build.dir}/${project.distname}_${version}.war" tofile="${deploy.dir}/AS.war" />
</target>

Related

Authentication Provider does not appear in the list of Authenticators in WebLogic Server Version: 12.1.3.0.0

I've just created an Authentication Provider for WebLogic Server Version: 12.1.3.0.0, put the jar in the lib of the domain, but does not appear in the List of Providers to select
<?xml version="1.0"?>
<!DOCTYPE MBeanType SYSTEM "commo.dtd">
<!-- MBean Definition File (MDF) for the MMR WS Authenticator See documentation at: http://docs.oracle.com/cd/E13212_01/wles/docs42/dvspisec/mdf_ref.html Since it is for an identity asserter, it must extend the weblogic.management.security.authentication.IdentityAsserter mbean. The Name and DisplayName must be the same. They specify the name that will appear on the console for this provider. Set the PeristPolicy to "OnUpdate" so that if an attribute value is changed, the new value is written to disk immediately. See the "Developing Security Services" manual for more info. Note that since this is an xml document, you can't use double quotes directly. Instead you need to use " Note that setting "Writeable" to "false" on an attribute makes the attribute read-only. The default is read-write. -->
-<MBeanType PersistPolicy="OnUpdate" Extends="weblogic.management.security.authentication.Authenticator" Package="fr.telecom.security.authentication.provider.mbean" Name="MMRWSAuthenticator">
<!-- You must set the value of the ProviderClassName attribute (inherited from the weblogic.management.security.Provider mbean) to the name of the java class you wrote that implements the weblogic.security.spi.AuthenticationProvider interface. You can think of the provider's mbean as the factory for your provider's runtime implementation. -->
<MBeanAttribute Name="ProviderClassName" Default=""fr.telecom.security.authentication.provider.MMRWSAuthenticationProviderImpl"" Preprocessor="weblogic.management.configuration.LegalHelper.checkClassName(value)" Writeable="false" Type="java.lang.String"/>
<!-- You must set the value of the Description attribute (inherited from the weblogic.management.security.Provider mbean) to a brief description of your provider. It is displayed in the console. -->
<MBeanAttribute Name="Description" Default=""MMR WS Authenticator"" Writeable="false" Type="java.lang.String"/>
<!-- You must set the value of the Version attribute (inherited from the weblogic.management.security.Provider mbean) to your provider's version. There is no required format. -->
<MBeanAttribute Name="Version" Default=""1.0"" Writeable="false" Type="java.lang.String"/>
</MBeanType>
This is my build.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!--
This project is not entirely handled by Maven, so be careful when generating the artifact, and use Ant with its default target.
-->
<project name="devices-ws-authentication-provider" default="all" basedir=".">
<echo>
Remember to execute the command below before doing an Ant:
%MW_HOME%\wlserver\server\bin\setWLSEnv.cmd
</echo>
<property name="src-java" value="src/main/java" />
<property name="mbean" value="src/mbean" />
<target name="all" depends="build" />
<target name="build" depends="clean,build.mdf,build.mjf" />
<target name="clean">
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${mbean}" includes="**/*"/>
</delete>
<delete file="target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" failonerror="false" />
<mkdir dir="target" />
<echo message="Clean finish" />
</target>
<!-- helper to build an MDF (mbean definition file) -->
<target name="build.mdf">
<java dir="${basedir}" fork="false" classname="weblogic.management.commo.WebLogicMBeanMaker">
<arg line="-files ${mbean}" />
<arg value="-createStubs" />
<arg line="-MDF src/main/resources/TntWSAuthenticator.xml" />
</java>
<echo message="Created Supporting Classes" />
<move todir="${src-java}/fr/telecom/security/authentication/provider/mbean" file="${mbean}/TntWSAuthenticatorImpl.java" />
<move todir="${src-java}/fr/telecom/security/authentication/provider/mbean" file="${mbean}//fr/telecom/security/authentication/provider/mbean/TntWSAuthenticatorMBean.java" />
</target>
<target name="build.mjf">
<mkdir dir="${mbean}" />
<copy todir="${mbean}">
<fileset dir="src/main/java">
<include name="**/*" />
</fileset>
<fileset dir="src/main/resources">
<include name="**/*" />
</fileset>
</copy>
<java dir="${basedir}" fork="false" classname="weblogic.management.commo.WebLogicMBeanMaker">
<arg line="-MJF target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" />
<arg line="-files ${mbean}" />
</java>
<echo message="Mbean JAR created." />
<!-- Do a small cleanup after building the JAR, in order to not have duplicated sources / classes in source paths -->
<!--
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${mbean}" includes="fr/telecom/security/authentication/provider/*.java" />
<fileset dir="${mbean}" includes="fr/telecom/domain/**/*.java" />
<fileset dir="${mbean}" includes="commo.dtd" />
<fileset dir="${mbean}" includes="rebel.xml" />
<fileset dir="${mbean}" includes="TntWSAuthenticator.xml" />
</delete>
-->
</target>
<target name="install" depends="build">
<copy todir="${user.home}/devices/lib" file="target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" />
</target>
</project>
opening the generated ear I see that there is some missing folders and files, like
/schemacom_bea_xml/element
/schemacom_bea_xml/namespace
/schemacom_bea_xml/system
/schemacom_bea_xml/type
/weblogic/
WLS providers is a nightmare, I have already developed two and now I am using Spring Security :) for the shake of simplicity and portability.
Well, I read your build.xml and it result strange for me, but the most obvious thing I see is that you lack the following argument:
<arg line="-targetNameSpace your.namespace.here" />
inside WebLogicMBeanMake ant rule. Try it, but if it does not work I will recommend you to redo. I can provide you some samples.
Edit
In response to your comment, I will include here a sample that I have already working in a real project.
This is a build.xml file to be run with and. To run this sample it is required to have WLS 12c installed. Before run ant buildAndDeploy you have to run ${WLS_HOME}/server/bin/setWLSEnv.sh script to prepare environment.
You have to adapt global properties at the beginning of the file.
<project name="ldap-db-provider" default="buildAndDeploy" basedir=".">
<description>Custom Authentication Provider Build</description>
<!-- global properties -->
<property name="base.dir" location="." />
<property name="bea.home" location="/opt/weblogic/Oracle/Middleware/Oracle_Home" />
<property name="mbean.maker" location="${bea.home}/wlserver/modules/com.bea.core.mbean.maker_2.1.0.0.jar" />
<property name="wls.home" value="${bea.home}/wlserver" />
<property name="wls.lib.dir" value="${wls.home}/server/lib" />
<property name="mbeantypes.dir" value="${wls.lib.dir}/mbeantypes" />
<property name="jar.file.name" value="your-name-for-the-provider-package.jar" />
<property name="src.dir" value="${base.dir}/src" />
<property name="provider.src.dir" value="${src.dir}/es" />
<property name="build.dir" value="${base.dir}/build" />
<property name="targetNamespace" value="your.target.name.space"/>
<property name="mbean.impl.name" value="YouBeanName" />
<property name="mbean.package" value="your/mbean/pacakage/name" />
<target name="updateSources">
<copy todir="${src.dir}/${mbean.package}" flatten="true">
<fileset dir="${build.dir}">
<include name="${mbean.impl.name}Impl.java" />
</fileset>
</copy>
<copy todir="${src.dir}/${mbean.package}" flatten="true">
<fileset dir="${build.dir}/${mbean.package}">
<include name="**/*.java" />
</fileset>
</copy>
</target>
<target name="buildAndDeploy" depends="clean">
<mkdir dir="${build.dir}" />
<copy todir="${build.dir}" flatten="true">
<fileset dir="${wls.lib.dir}">
<include name="commo.dtd" />
</fileset>
</copy>
<copy todir="${build.dir}" flatten="true">
<fileset dir="${provider.src.dir}">
<include name="**/*.xml" />
<include name="**/*.java" />
</fileset>
</copy>
<java
classname="weblogic.management.commo.WebLogicMBeanMaker"
fork="true" failonerror="true">
<!-- <classpath refid="build.path"/> -->
<arg line="-files ${build.dir}" />
<arg value="-createStubs" />
<arg line="-MDF ${build.dir}/${mbean.impl.name}-Mbean.xml" />
</java>
<echo message="Created Supporting Classes" />
<java
classname="weblogic.management.commo.WebLogicMBeanMaker"
fork="true" failonerror="true">
<!-- <classpath refid="build.path"/> -->
<arg line="-MJF ${build.dir}/${jar.file.name}" />
<arg line="-targetNameSpace ${targetNamespace}" />
<arg line="-files ${build.dir}" />
</java>
<echo message="Created Mbean Jar" />
<!-- Deploy the sample security providers -->
<copy todir="${mbeantypes.dir}" flatten="true">
<fileset dir="${build.dir}">
<include name="${jar.file.name}" />
</fileset>
</copy>
</target>
<target name="clean">
<delete quiet="true" dir="${build.dir}" />
<delete>
<fileset dir="${src.dir}/${mbean.package}" includes="**/*.java" />
</delete>
<delete quiet="true" file="${mbeantypes.dir}/${jar.file.name}" />
</target>
</project>
Hope it helps!

Issue while creating war using ant with Jenkins

I have a ant build script which creates a war file. The file content are as follows.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestProj" default="war" basedir=".">
<property name="project-name" value="${ant.project.name}" />
<property name="builder" value="IaasTeam" />
<property name="war-file-name" value="${project-name}.war" />
<property name="source-directory" value="src" />
<property name="classes-directory" value="build/classes" />
<property name="web-directory" value="WebContent" />
<property name="web-xml-file" value="WebContent/WEB-INF/web.xml" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="catalina.home" value="../../outside/project/lib"/>
<tstamp prefix="build-info">
<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
</tstamp>
<property name="build-directory" value="build" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${catalina.home}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath" />
</target>
<target name="war" depends="clean,compile">
<mkdir dir="${build-directory}" />
<delete file="${build-directory}/${war-file-name}" />
<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
<classes dir="${classes-directory}" />
<fileset dir="${web-directory}">
<!-- Need to exclude it since webxml is an attribute of the war tag above -->
<exclude name="WEB-INF/web.xml" />
</fileset>
<manifest>
<attribute name="Built-By" value="${builder}" />
<attribute name="Built-On" value="${build-info.current-date}" />
<attribute name="Built-At" value="${build-info.current-time}" />
</manifest>
</war>
</target>
I am using Jenkins as a build server (this is hosted on different machine kind of DEV environment).
I also use Gitlab as a repository and after pushing the latest code I have a hook for Jenkins job which gets triggered automatically and calls this build.xml.
Now the issues here is that when I run this script on my local machine everything works well but when Jenkins execute this it fails during the compilation phase giving me below error.
compile:
[mkdir] Created dir: /app/infra/jenkins/workspace/TestProj/build/classes
[javac] Compiling 49 source files to /app/infra/jenkins/workspace/TestProj/build/classes
BUILD FAILED
/app/infra/jenkins/workspace/TestProj/build.xml:27: /app/infra/jenkins/outside/project/lib does not exist.
The reason for this issue is the build server does not have any directoy called outside/project/lib.
The only reason of adding this directory in my build.xml is to have the container specific jar files ready for compiling.
How can I fix this issue?
Do I need to copy container specific jars on my build server? Or is there any way to tell Jenkins that not to copy this external jars but just use them for compilation.
Where would Jenkins find the jars? They need to be accessible otherwise your build will fail. If you don't want to have the files checked in (which is very sensible), you could use Apache Ivy to download them for you.
This is the most common way of handling the situation you're having. Using a dependency management framework like Ivy (or Maven, or similar) will save you a lot of headaches down the line. I recommend you have a look at their tutorial. After you set it up, your ant build will take care of downloading the files you need.

ANT/JAVA: Config to include library files in the war file

Pretty new to ANT and building using it. I have a Java-Jersey rest project and i have included the Jersey libraries under WEB-INF/lib.
I have a build.xml for building/compiling the project.
<?xml version="1.0"?>
<project name="Ant-Test" default="Main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="lib.dir" location="" />
<property name="build.dir" location="bin" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build directory-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!-- Creates the build directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
<jar destfile="${build.dir}/CrunchifyRESTJerseyExample.jar" basedir="${build.dir}"/>
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
</war>
</target>
<target name="Main" depends="compile">
<description>Main target</description>
</target>
</project>
With this, i am not getting the library files in the war file. What should i add to get it in the war?.
If you take a look on the Ant war task you can specify a <lib> element with the jars that are going to be put under WEB-INF/lib folder. So try this:
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
<lib dir="${lib.dir}">
<exclude name="jdbc1.jar"/> <!-- Exclude here jars you don't want -->
</lib>
</war>
Note: You should set your property at the begining of your script for the above task to work properly:
<property name="lib.dir" location="lib" /> <!-- Or whatever you call your project folder with the jars-->
try including fileset element as child of war element
<fileset dir="${home.dir}/WEB-INF/libDirectory/*">
<include name="**/*"/>
</fileset>

Is there an ant task to add a .war to an existing exploded ear

Have a build process that can't be edited and need to pack another war in the the ear that is generated. The ear is exploded so it's just a matter of copying the war file into it but the application.xml needs to be updated so I'd like to find an ant task that will do this. Anyone know of one that will work?
ended up just doing :
<replace file="${j2ee.build.dir}/${j2ee.app..name}/META-INF/application.xml" token="</application>" value="<module><web><web-uri>admin.war</web-uri><context-root>/admin</context-root></web></module></application>"/>
Rather hackish but couldn't come up with another way to edit the file easily
The idea is to create the war and put the file into the ear directory, see the following build.xml code
<?xml version="1.0"?>
<project name="Add war to ear example" default="all" basedir=".">
<target name="init">
<property name="root.directory" value="${basedir}"/>
<property name="classdir" value="${root.directory}/build/src"/>
<property name="src" value="${root.directory}/src"/>
<property name="web" value="${root.directory}/web"/>
<property name="deploymentdescription" value="${root.directory}/build/deploymentdescriptors"/>
<property name="war.file" value="test.war"/>
<property name="ear.file" value="test.ear"/>
<property name="ear.directory" value="${root.directory}/build/ear"/>
<property name="war.directory" value="${root.directory}/build/war"/>
<!-- Create Web-inf and classes directories -->
<mkdir dir="${war.directory}/WEB-INF"/>
<mkdir dir="${war.directory}/WEB-INF/classes"/>
<!-- Create Meta-inf and classes directories -->
<mkdir dir="${ear.directory}/META-INF"/>
</target>
<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildEar"/>
<!-- Compile Java Files and store in /build/src directory -->
<target name="build" >
<javac srcdir="${src}" destdir="${classdir}" debug="true" includes="**/*.java" />
</target>
<!-- Create the War File -->
<target name="buildWar" depends="init">
<copy todir="${war.directory}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>
<copy todir="${war.directory}/WEB-INF">
<fileset dir="${deploymentdescription}" includes="web.xml" />
</copy>
<copy todir="${war.directory}">
<fileset dir="${web}" includes="**/*.*" />
</copy>
<!-- Create war file and place in ear directory -->
<jar jarfile="${ear.directory}/${war.file}" basedir="${war.directory}" />
</target>
<!-- Create the War File -->
<target name="buildEar" depends="init">
<copy todir="${ear.directory}/META-INF">
<fileset dir="${deploymentdescription}" includes="application.xml" />
</copy>
<!-- Create ear file and place in ear directory -->
<jar jarfile="${root.directory}/${ear.file}" basedir="${ear.directory}" />
</target>
</project>
Won't <copy> do the job?
What about using <xslt> to modify application.xml then?

Java applet, add jar files to the manifest file

I am doing some thing obvious wrong. I have a simple applet which needs to upload files to server. I have written an ant script to build the jar file. However, the manifest.mf has class-path split into multiple lines.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Class-Path: lib/commons-codec-1.3.jar lib/commons-httpclien
t-3.1.jar lib/commons-logging-1.0.4.jar lib/plu
gin.jar
Created-By: 14.3-b01-101 (Apple Inc.)
My build.xml is :
<project name="ScreenShot" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src" />
<property name="build" location="build" />
<property name="dist" location="dist" />
<target name="init">
<!-- Create the time stamp -->
<tstamp />
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
</target>
<target name="compile" depends="init" description="compile the source ">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}">
<classpath>
<pathelement path="${classpath}" />
<pathelement path="lib/commons-codec-1.3.jar:lib/commons-httpclient-3.1.jar:lib/plugin.jar" />
</classpath>
</javac>
</target>
<target name="dist" depends="compile" description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}" />
<copy todir="${build}/lib">
<fileset dir="lib/" />
</copy>
<path id="libs.project">
<!-- lib.home contains all jar files, in several subdirectories -->
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
<manifestclasspath property="jar.classpath" maxParentLevels="1" jarfile="build/ScreenShot.jar">
<classpath refid="libs.project" />
</manifestclasspath>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="/Users/firemonk/red5/webapps/whiteboard/ScreenShot.jar" basedir="${build}">
<manifest>
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
</target>
<target name="clean" description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>
Check the value of ${jar.classpath}. It seems its value itself is in multiple lines.
Does it not work? It's a bit odd that there are big spaces between each classpath entry but it looks valid.
The manifest specification states that lines must be no longer than 72 bytes and that longer lines should be split and continued on the next line with a leading space.

Categories