Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ant replace text in files inside a jar/ear/war?

Tags:

java

ant

I want to build my ear file once and then use ant to change some settings in application.xml, property files etc.

Is there way to do this with ant?

[edit] Just found this

How do I modify a file in a jar file using ANT?

like image 992
blank Avatar asked Nov 04 '11 12:11

blank


People also ask

What is the difference between JAR war and EAR files?

An EAR file requires a fully Java Platform, Enterprise Edition (Java EE)- or Jakarta Enterprise Edition (EE)-compliant application server, such as WebSphere or JBoss, to run. A WAR file only requires a Java EE Web Profile-compliant application server to run, and a JAR file only requires a Java installation.

What is WAR file and EAR file?

The WAR file is a file that contains files such as a servlet, JSP, HTML, JavaScript, etc. that are necessary to develop web applications. EAR is a Java EE file that packages one or more modules into a single archive to deploy them on to an application server.

What are EAR files used for?

An EAR file is a critical piece in deploying a service application to a production server. An enterprise archive (EAR) file is a compressed file that contains the libraries, enterprise beans, and JAR files that the application requires for deployment.


2 Answers

The only way you can modify a file inside your jar or ear is to use the <unzip> task, use the <replace> task to modify the fields in the file, and then rezip the file back up with either the <zip> or <jar>/<ear> task.

There are several ways you can handle this without having to unzip and rezip your ear/jar/war files:

  • The preferred method is to setup your application server, so it can find your properties outside of the ear itself. It is also possible to configure the application.xml file not to use relative directories when specifying locations instead of specifying locations from the root of the machine. By removing embedded environment information from your ear, you can use the same earfile on all of your environments.

  • We, unfortunately, are unable to do the above and must provide separate ear files for each environment. We use Jenkins as our continuous build server. When Jenkins does our builds, we build multiple ears, one for each environment, at the same time. This way, as we move from Dev to QA to STAGE to Production, we can at least refer to the same build number. We compile once, then use the AntContrib <foreach> task to ear up the earfile with the correct properties file settings and the correct application.xml file. We use <filterset> in our <copy> task to modify the properties and application.xml as we build the ear.

like image 60
David W. Avatar answered Sep 28 '22 06:09

David W.


You can do something like this

<zip destfile="tmp.jar" >
  <zipfileset src="lib/myjar.jar" excludes="org/example/My*.class" />
  <zipfileset dir="bin" includes="org/example/My*.class"  />
</zip>
<move file="tmp.jar" tofile="lib/myjar.jar"/>

In this example we create a tmp.jar using myjar.jar as the source but excluding all classees beginning My in the org/example directory. We then add our new version in from the bin directory. We then replace the jar file with our new version.

like image 36
David George Avatar answered Sep 28 '22 06:09

David George