6

At the end of my ant build id like it to call the equivalent of the command line call

mvn install:install-file -Dfile=my.jar -DgroupId=com.company.project -DartifactId=my_project -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

so that it will add the newly built jar to a maven repository which another project will rely on.

Ive tried using the maven-ant-task and have added the maven-ant-task jar to the ant built project and the following code to the build.xml:

<target name ="minstall" depends="jar">
  <artifact:pom id="maven_install" file="maven_install.xml" />
  <artifact:install file="${out.dir}/my_project.jar">
      <pom refid="maven_install"/>
  </artifact:install> 
</target>

but seem to be missing something as it wont work for me. To begin with i get the error in the build.xml (ant build file) saying

The prefix "artifact" for element "artifact:pom" is not bound.

What am I doing wrong. I am fairly new to ant?

On a realted question what is the purpose of the associated POM file? I would not normally have a POM in this project as it is an ant build

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Rob McFeely
  • 2,823
  • 8
  • 33
  • 50

4 Answers4

3

Perhaps maven-ant-task jar is not installed, i.e. not in your ant CLASSPATH. You can follow this instruction for this.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • i have added the maven ant task jar to ant lib dir, added the the dir to the windows classpath and also added the code (http://maven.apache.org/ant-tasks/installation.html) to a local pom file as per instructions. Still no joy – Rob McFeely Jan 11 '11 at 11:56
  • Can you recheck if your build.xml has the xml namespace – Raghuram Jan 11 '11 at 12:08
  • Raghuram is right this is related to maven-ant-task. See http://stackoverflow.com/questions/1490914/how-to-get-project-version-from-mavenss-pom-in-ant – Umut Utkan Jan 11 '11 at 15:20
3

As mentioned previously, you need to make sure the tasks are defined in your ant script, and the artifact namespace is understood.

The POM file is used (in this case) to tell the Maven repositories the dependencies of the JAR you are putting in the repository. The POM should also specify the JAR's identification information (groupId, artifactId, version number, license, etc.).

Strictly speaking, you do not need an external POM, you could define the information in your build.xml file as follows:

<!-- Assuming tasks defined, and 'artifact' namespace exists -->
<artifact:pom id="maven_install" groupId="com.whatever" artifactId="some-jar"
              version="1.0" packaging="jar">
    <dependency groupId="..." artifactId="..." version="..."/>
    <dependency groupId="..." artifactId="..." version="..."/>
    <license name="apache" url="http://www.apache.org"/> <!-- can be omitted -->
</artifact:pom>

<target name ="minstall" depends="jar">
    <artifact:install file="${out.dir}/my_project.jar" pomRefId="maven_install"/>
</target>

When you install the JAR in the 'minstall' task, the POM should be generated with the appropriate dependencies in the local Repository.

GKelly
  • 3,835
  • 4
  • 38
  • 45
  • 4
    There's currently a bug in the install task above if you use an in-memory POM file. The artifact will be installed as 'super-pom', not the artifactId you specified. The Maven devs are working on this. (Mentioned in question http://stackoverflow.com/questions/4887018/artifactinstall-pushes-the-super-pom-instead-of-the-pom-i-define) – GKelly Mar 23 '11 at 09:11
1

That message means you are missing an xmlns:artifact attribute in your build.xml. Have a look at the installation page in the docs for an example.

As to the purpose of the POM file, it's mostly metadata so that maven can figure out dependencies properly. In a real maven build it also describes how to build, test and package. But in your case all that is done by ant instead.

Dominic Mitchell
  • 11,861
  • 4
  • 29
  • 30
-2

I think that it makes no sense to put such commands in Ant's build.xml. If you want to have your jar file installed in your maven repo just use mvn install command.

Besides that, I guess that you are somehow confusing the purpose of Maven and Ant tools in your project. What I'd suggest is to use Maven as your main build tool. You can configure invokation of Ant targets in your POM file if you really need that. Personally, I think it is the best solution to have Ant called by Maven. Maven goals (such as clean, test, package, install and so on) are very simple to use and powerful (I guess that you can read it in every Maven tutorial).

Lukasz
  • 7,572
  • 4
  • 41
  • 50
  • 4
    Baran. Perhaps his project is using ant and a different project uses maven. – Raghuram Jan 11 '11 at 11:12
  • thats correct Raghuram I have a number of new projects using maven and a number of old legacy projects using ant. These legacy projects must have their jars added to the local maven repo after each build. I tried having an ant build build.xml called by maven however this didnt work reliably. While it called the ant build.xml successfully it quickly threw up exceptions during the ant build (ant build works fine through ant alone). It seems that having maven call ant is different to having ant called on its own. Some properties are being passed by the maven instance which I didnt ask for. – Rob McFeely Jan 11 '11 at 11:53
  • Although id still like to solve this I have worked around it for now by calling a batch script from the the ant build file using the exec command. this script invokes the mvn install command for installing jar files to the local repo. – Rob McFeely Jan 11 '11 at 11:59