text replace in xml using ant - ant

I want to change the version number in a xml file using ant.
I tried ant's replace task but it's not working.
The xml file I have looks like this.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org<br>/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz</groupId>
<artifactId>proj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

You can use copy task.
http://ant.apache.org/manual/Tasks/copy.html
abc.template.xml:
<abc version="#VERSION#">
<item name="xxxxx"/>
</abc>
ant_script:
<copy file="abc.template.xml"
tofile="abc.xml"
filtering="yes" overwrite="yes">
<filterset>
<filter token="VERSION" value="1.0"/>
</filterset>
</copy>

I am not sure, but it appears your problem is caused by wrong file or wrong syntax.
I have below content in my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>#VERSION</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
</project>
See below my build.xml
<project name="Test" default="replaceContent" basedir=".">
<target name="replaceContent">
<replace file="src/pom.xml" token="#VERSION" value="5.0.0.0"/>
</target>
</project>
Now If i run $ ant replaceContent, it replace my token #VERSION with whatever i specify in build file as 'value'. So after successful execution, <modelVersion> value in pom.xml will change to 5.0.0.0
Next time when you post any question, also mention details of error.

Related

maven-antrun-plugin generating emty ddl file

I would like to generate schema sql script with maven.
Here is my persistance file :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="mypersistance"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>
<property name="hibernate.archive.autodetection" value="class"></property>
</properties>
<description>Persistance descriptor</description>
<class>test.sofiane.beans.Code</class>
</persistence-unit>
</persistence>
hibernate configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="mySessionFactory">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.default_schema">public</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
plugin in the pom
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<!-- Hibernatetool will generate everything before running tests -->
<phase>compile</phase>
<configuration>
<target>
<echo message="Ant target, through maven-antrun-plugin, started" />
<property name="maven_compile_classpath" refid="maven.compile.classpath" />
<property name="maven_test_classpath" refid="maven.test.classpath" />
<path id="hibernatetool.path">
<pathelement path="${maven_compile_classpath}" />
<pathelement path="${maven_test_classpath}" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="hibernatetool.path" />
<property name="generatedByHibernate.outputDirectory"
value="${project.build.directory}/generated/hibernatetool" />
<mkdir dir="${generatedByHibernate.outputDirectory}" />
<hibernatetool destdir="${generatedByHibernate.outputDirectory}">
<classpath>
<path location="${project.build.directory}/classes/test/sofiane/beans" />
</classpath>
<configuration
configurationfile="${project.build.directory}/classes/hibernate.cfg.xml" />
<hbm2ddl export="true" drop="true" create="true"
outputfilename="helloworld.ddl" format="true" />
</hibernatetool>
<echo message="Ant target, through maven-antrun-plugin, terminated" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The pom works fine and generate helloworld.ddl but empty unfortunately !
Any idea please ?
My first advice is that you should better use the hibernate3-maven-plugin instead of the maven-antrun-plugin because it includes everything that you need and is a lot more simple to use than all the configuration that you had to write with the maven-antrun-plugin (see here for more information about the hibernate3-maven-plugin).
And then, to fix your problem, I think that you can find the answer to your problem in this post and in the answer because it seems that all the elements given will lead you to make your configuration work good.
And don't forget to bind the run of the hibernate3-maven-plugin after the compile phase, on the process-classes phase for example (see Lifecycle Reference) and then just run mvn process-classes.

Maven to SVN repository

I am trying to connect maven to SVN repository using valid credentials. My pom.xml file is as follows,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven.myfirstproject</groupId>
<artifactId>myfirstproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<scm>
<connection>scm:svn:http://d-113017553/svn/PRONTO/trunk/dev</connection>
<developerConnection>scm:svn:http://d-113017553/svn/PRONTO/trunk/dev</developerConnection>
</scm>
<servers>
<id>central</id>
<username>keerthana</username>
<password>keerthana</password>
<url>http://d-113017553/svn/PRONTO/trunk/dev</url>
</servers>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-scm-plugin</artifactid>
<version>1.8.1</version>
</plugin>
</build>
</project>
When i run it i get the following errors,
**1)coreException : could not calculate build plan: plugin org.apache.maven.plugins:maven-compiler:2.3.2 or one of its dependencies could not be resolved:Failed to read artifact descriptor for org.apache.maven.plugins:maven-compiler-plugin:jar:2.3.2
2)Plugin execution not covered by lifecycle configuration:org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile(execution:default-testCompile,phase:test-compile)**
Would anyone help me out to solve the above errors pls?????
First your pom.xml file can contain the information about VCS but you have to follow the schema for the pom.xml file which can get a good impression about here.
Furthermore it's good practice to indent XML file correctly like this:
<scm>
<connection>scm:svn:http://svn.server.com/svn/my-project/trunk</connection>
<developerConnection>scm:svn:https://svn.server.com/svn/my-project/trunk</developerConnection>
<url>http://svn.server.com/websvn/my-project</url>
</scm>
Apart from the above the things like this:
<servers>
<id>central</id>
<username>keerthana</username>
<password>keerthana</password>
<url>http://d-113017553/svn/PRONTO/trunk/dev</url>
</servers>
do not belong into the pom.xml. They should be put into the setting.xml file in your home folder (usually $HOME/.m2/settings.xml). And of course should you never put credentials into your pom file.

Updating versions in multi-module Maven project

I am trying to release our multi-module Maven project and I can't figure out how to properly update the version numbers in our POMs.
The maven release plugin doesn't update versions in the parent POM dependencyManagement section - that seems to be a known bug.
I tried to use the versions plugin, but I have two issues with that:
the plugin only updates my toplevel POM; probably because the multi-module project POM is not the paremt POM of the modules's POMs
more significantly, how can I mimic the behavior of the release plugin to automatically compute the versions to set (e.g. 1.0.0) from the current version in the POMs (e.g. 1.0.0-SNAPSHOT)... without giong into perl scripting?
Any suggestion that doesn't involve scripting, and doesn't require me to declare dependency versions outside of dependencyManagement would be very appreciated!
Here are the 4 POMs (module POMs in their module subdirectory):
============ pom-parent.xml ==================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<version>2.5</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
============= multi-module pom: pom.xml ================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>multi-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<scm>
<developerConnection>scm:svn:https://svnserver.fairisaac.com:8443/nowhere</developerConnection>
</scm>
</project>
============== module1 POM: module1/pom.xml ================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
============= Module 2 POM: module2/pom.xml =============
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
</dependency>
</dependencies>
</project>
========= Edit ==============
... and it gets better, if I replace the explicit version in the dependencyManagement section with
<version>${project.version}</version>
... maven 3.0.4 fails with an NPE:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.478s
[INFO] Finished at: Fri Apr 12 12:09:08 CEST 2013
[INFO] Final Memory: 9M/120M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project multi-
module: Execution default-cli of goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare failed. NullPointerExcep
tion -> [Help 1]
This is not a bug, the first pom.xml you showed is outside your multi-module project. The maven-release-plugin will consider it an external dependency, thus it won't change the version.
But there is a flaw in your design, you have to move the
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
to your multi-module pom.
I'm assuming your idea is to declare the module version in one place and share it among all modules. So, the module version goes into the multi-module pom dependencyManagement.
Usually external poms like this are used to put company information, repository paths, things like this, and they are shared by many different projects.

Why can't I use inherited references outside a target on ant (1.8.1)?

All I want to do is set a reference to a path on a parent build.xml and use it on a child build.xml (called with <ant>). Here's the simplest example I could create:
build-parent.xml
<?xml version="1.0"?>
<project name="parent" default="do-parent">
<target name="do-parent">
<path id="inherited.path"/>
<ant antfile="build-child.xml" inheritrefs="false" inheritall="false">
<reference refid="inherited.path"/>
</ant>
</target>
</project>
build-child.xml
<?xml version="1.0"?>
<project name="child" default="do-child">
<pathconvert property="converted.path" refid="inherited.path"/>
<target name="do-child"/>
</project>
I run ant.bat -f build-parent.xml
and get: Reference inherited.path not found.
If I change build-child.xml to be:
<?xml version="1.0"?>
<project name="child" default="do-child">
<target name="do-child">
<pathconvert property="converted.path" refid="inherited.path"/>
</target>
</project>
It works fine...
Obviously I could work around this in several ways, but I wanted to do this the proper way. What am I doing wrong?
This behavior has been encountered before:
Passing References from Master Build File to Child
I couldn't find anything that suggests whether this behavior is by design.

How to include a file in ivy.xml?

I have a fairly large ivy.xml containing a number of configurations which are the same for a number of projects.
I would like to break out this large repetitive section in to a common include file. Somehow I can't find any documentation describing that this can be done.
Anyone who has an idea whether this is doable?
EDIT: After some further thinking, I think this is not doable on purpose. An Ivy file is meant to be one cohesive unit and should contain no file based references, only references to other ivy modules...
You could create an ivy meta-module, which depends upon all of those common packages, and then have all your other projects resolve the common libraries through transitive dependency:
<?xml version="1.0"?>
<ivy-module version="2.0">
<info organisation="com.example" module="common-libs"/>
<configurations>
<conf name="runtime" transitive="true" visibility="public" />
<conf name="master" transitive="true" visibility="public" />
<conf name="compile" transitive="true" visibility="public" />
<conf name="default" transitive="true" visibility="public" extends="master" />
</configurations>
<dependencies>
<dependency org="oracle" name="ojdbc14_g" rev="10.2.0.3"
conf="compile->compile(*),master(*);runtime->runtime(*);master->master(*)"/>
<dependency org="tomcat" name="servlet-api" rev="6.0.16"
conf="compile->compile(*),master(*);runtime->runtime(*);master->master(*)"/>
<dependency org="junit" name="junit" rev="4.3"
conf="compile->compile(*),master(*);runtime->runtime(*);master->master(*)"/>
</dependencies>
</ivy-module>
And for a typical project:
<?xml version="1.0"?>
<ivy-module version="2.0">
<info organisation="com.example" module="myproject"/>
<configurations>
<conf name="runtime" transitive="true" visibility="public" />
<conf name="master" transitive="true" visibility="public" extends="runtime"/>
<conf name="compile" transitive="true" visibility="public" />
<conf name="default" transitive="true" visibility="public" extends="master" />
</configurations>
<dependencies>
<dependency org="com.example" name="common-libs" rev="latest.release"
conf="compile->compile(*),master(*);runtime->runtime(*);master->master(*)"/>
</dependencies>
</ivy-module>
Here I'm using the traditional configuration naming conventions from the POM->Ivy translations of the Maven resolver, though you could map the configuration names in any way that made sense to you. I tend to use the ivy:install task to copy Maven modules into our Ivy repository, so I use the default ivy.xmls for the most part. If you're using IvyRoundup, you'll primarily want to map the "default" configuration transitively.
If you are using ivy from ant and you are running Ant 1.6 or later, you could use the <import> task to include build file fragments within your ant build file. The referenced files have to be complete Ant build files, though:
<?xml version="1.0"?>
<project name="my-project" default="usage" basedir=".">
<target name="setup">
...
</target>
<import file="./common.xml"/>
...
</project>
You could also use standard XML syntax from within your ivy.xml or build.xml file:
<?xml version="1.0"?>
<!DOCTYPE project [
<!ENTITY common SYSTEM "common.xml">
]>
<project name="my-project" default="usage" basedir=".">
<target name="setup">
...
</target>
&common;
...
</project>
This will literally include the contents of common.xml where you've placed the &common; entity.
(The filename common.xml in this example is resolved relative to the containing XML file by the XML parser. You may also use an absolute file: protocol URI.)

Resources