Could not load definitions from resource antlib.xml. Understanding the problem and fix worklow

By neokrates, written on October 15, 2015

howto

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 142 views
Rate it
Ad
Poll
  • What build management tool does your project or firm use?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

‘Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.’ This and similar output seems to have many reasons. Additional problem is that depending on maven version same pom will work or fail. Here I describe the simple way to understand the problem and fix it.

1

Problem description

By now you probably have pom.xml with smth. like this :

...
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.4</version>
				<executions>
					<execution>
						<phase>process-resources</phase>
						<id>Use and contrib</id>
						<configuration>
							<tasks>

						    <taskdef resource="net/sf/antcontrib/antlib.xml"/>
								
								<for list="dir1,dir2,dir3" param="dir">
								  <sequential>
									<echo>Dir @{dir}</echo>
								  </sequential>
								</for>

							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>			
...

You also have some kind of dependencies to ant-contrib library in your pom. Later I will list the version which works well for m2 and m3 versions, just in case you need a reference.

What you get is the ‘Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.’ (Or from properties) Which means just that plugin and ant have no class library in the classpath they use, which contains the .xml or .property file you point to. It is either not there or you refer it wrong way.

Lets go through the options.


2

Ensure that proper ant-contrib is in maven repo

1.0b3 is the version ant-contrib you need. Check it here.
Check your ${USER_HOME}\.m2\repository\ant-contrib\ant-contrib\1.0b3.

If you don’t have it, add this part to you normal maven dependency section :


	<dependencies>


		  <dependency>
			
			<groupId>ant-contrib</groupId>
				<artifactId>ant-contrib</artifactId>
				<version>1.0b3</version>
				<exclusions>
					<exclusion>
						<groupId>ant</groupId>
						<artifactId>ant</artifactId>
				</exclusion>
				</exclusions>
		  </dependency>
		  
		  <dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant-nodeps</artifactId>
			<version>1.8.1</version>
		  </dependency>
		  
	</dependencies>	  

Another option is to add it in the plugin dependency section like this

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.4</version>
				<executions>
...
				</executions>
				<dependencies>
				  <dependency>
					<groupId>ant-contrib</groupId>
					<artifactId>ant-contrib</artifactId>
					<version>1.0b3</version>
					<exclusions>
					  <exclusion>
						<groupId>ant</groupId>
						<artifactId>ant</artifactId>
					  </exclusion>
					</exclusions>
				  </dependency>
				  <dependency>
					<groupId>org.apache.ant</groupId>
					<artifactId>ant-nodeps</artifactId>
					<version>1.8.1</version>
				  </dependency>
				</dependencies>				
			</plugin>

you can try it of course, but interesting thing is, it worked with maven 3 for me but not with maven 2. With maven 2 it didn’t got downloaded into local repository at all. So, i used standard dependency. It is simpler and works with all maven versions.

3

Print the classpaths available to your ant plugin, tune maven until classpath is correct

Now, you must have downloaded proper ant-contrib in local repo. What remains is to ensure that it is available in classpath and you properly refer it.

Have your antrun plugin containing only this block. This will print what’s available in the classpaths for the plugin :

...
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.4</version>
				<executions>
					<execution>
						<phase>process-resources</phase>
						<id>Use and contrib</id>
						<configuration>
							<tasks>

							<property name="compile_classpath" refid="maven.compile.classpath"/>
							<property name="runtime_classpath" refid="maven.runtime.classpath"/>
							<property name="test_classpath" refid="maven.test.classpath"/>
							<property name="plugin_classpath" refid="maven.plugin.classpath"/>

							<echo message="compile classpath: ${compile_classpath}"/>
							<echo message="runtime classpath: ${runtime_classpath}"/>
							<echo message="test classpath:    ${test_classpath}"/>
							<echo message="plugin classpath:  ${plugin_classpath}"/>	

							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>			
...

Now, that output is criteria simple enough to work with. If you see the ${USER_HOME}\.m2\repository\ant-contrib\ant-contrib\1.0b3\ant-contrib-1.0b3.jar library in that output, preferably compile_classpath, you just need to refer it. If not, you need to analyze why the library which is in dependency section doesn’t get included in this paths. It is supposed to. One reason why not might be that you have parents and more complex configuration from them which breaks things. Try to only have tis pom without parent and complex dependencies. Reduce it to minimum complexity. In the end you must see the ${USER_HOME}\.m2\repository\ant-contrib\ant-contrib\1.0b3\ant-contrib-1.0b3.jar in the ant output here.

4

Add the correct classpath to ant

By now, you know that ant-contrib library is available, and you made sure it is in the classpath available to ant-contrib plugin at execution time. It only remains to point to it correctly.

Just add to your plugin execution, like :

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.4</version>
	<executions>
		<execution>
			<phase>process-resources</phase>
			<id>Use and contrib</id>
			<configuration>
				<tasks>
				
				<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.compile.classpath" />
				...

You can also use other paths like maven.plugin.classpath. There is certainly a maven best practice to apply the correct classpath in this case.
By now it should work for you and you have the criteria to debug this problem if it fails for any reason.

Have fun!

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +1 (from 1 vote)
1 votes 'YES'  0 votes 'NO'

LEARN MORE (amazon bookstore)

TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


Leave a Reply