How to use artifact:writepom and artifact:pom? - maven-2

I've been trying to use writepom using this http://maven.apache.org/ant-tasks/examples/write-pom.html as a reference and have been having problems. I'm essentially just trying to test whether it will actually work so the POM file is quite bare. See below.
<project name="CreatePOMStructure" basedir="./test" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<description>
Test Script
</description>
<path id="maven-ant-tasks.classpath" path="/usr/local/apache-ant-1.8.1/lib/maven-ant-tasks-2.1.1.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath" />
<artifact:pom id="maven-pom" groupId="com.cgi.wealth" artifactId="maven-pom-setup" version="1.0" name="maven-setup">
<license name="apache" url="http://www.apache.org"/>
<dependency groupId="junit" artifactId="junit" version="4.1" scope="test"/>
<dependency groupId="org.codehaus.plexus" artifactId="plexus-utils" version="1.5.5"/>
</artifact:pom>
<artifact:writepom pomRefId="mypom1" file="mypom1.xml"/>
</project>
I get this error when I try to run ant
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-antrun-plugin:1.0:run
(compile) on project maven-setup:
Error executing ant tasks: The
following error occurred while
executing this line:
/maven-setup/scripts/build.xml:11:
java.lang.NoSuchMethodError:
org.apache.maven.settings.RuntimeInfo.(Lorg/apache/maven/settings/Settings;)V
-> [Help 1]
I'm not sure if it's relevant, but before I added the typedef I was getting this error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-antrun-plugin:1.0:run
(compile) on project maven-setup:
Error executing ant tasks: The
following error occurred while
executing this
line:/maven-setup/scripts/build.xml:9:
Could not create task or type of type:
antlib:org.apache.maven.artifact.ant:pom.
Ant could not find the task or a class
this task relies upon.
Sorry for the most likely basic question, but I can't seem to fix this myself.
[EDIT]
Here's the pom.xml file which I use to run the ant build.
<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.cgi.wealth</groupId>
<artifactId>maven-setup</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>compile</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<ant antfile="${basedir}/scripts/build.xml" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-ant-tasks</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
The problem with the project only exists when running the maven task "mvn generate-sources" (see pom.xml above). When I just run "ant" it builds successfully. Any insight is greatly appreciated.

This script runs fine, provided you've put maven-ant-tasks-2.1.1.jar into the same directory where your build.xml lives.
The errors you are encountering tell me that the path may be incorrect.
Also, it's better not to set basedir attribute of your project and use a default ( current directory of the build.xml )
Last, but not least, artifact:writepom pomRefId should match the id of artifact:pom.
Below is the script:
<project
name="CreatePOMStructure"
default="default"
xmlns:artifact="antlib:org.apache.maven.artifact.ant"
>
<path id="maven-ant-tasks.classpath" path="maven-ant-tasks-2.1.1.jar" />
<typedef
resource="org/apache/maven/artifact/ant/antlib.xml"
uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath"
/>
<target name="default">
<artifact:pom id="maven-pom"
groupId="com.cgi.wealth"
artifactId="maven-pom-setup"
version="1.0"
name="maven-setup"
>
<license name="apache" url="http://www.apache.org"/>
<dependency
groupId="junit"
artifactId="junit"
version="4.1"
scope="test"
/>
<dependency
groupId="org.codehaus.plexus"
artifactId="plexus-utils"
version="1.5.5"
/>
</artifact:pom>
<artifact:writepom pomRefId="maven-pom" file="mypom1.xml"/>
</target>
</project>

Related

How to run test cases using robot framework jar file?

I tried running the testcases by using robotframework-2.8.6 jar like below
java -jar robotframework-2.8.6.jar testcases
But Its not recognizing the selenium2keywords. How do I use the selenium2library with robotframework jar ?
The easiest (and most robust/enhanceable) way to use the robot framework jar file is through the maven plugin.
(I'm assuming here that you have a maven runtime)
Just create a pom file that uses the plugin and run it using mvn install
Adding selenium 2 becomes just a matter of adding a dependency to the pom file.
Example (with selenium 2) which contains some useful tricks in it as well.
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shtand</groupId>
<artifactId>robot-framework</artifactId>
<version>5.5.1</version>
<properties>
<logDir>${project.build.directory}/logs</logDir>
<webdriver.chrome.driver>bin\\chromedriver.exe</webdriver.chrome.driver>
</properties>
<dependencies>
<dependency>
<groupId>com.github.markusbernhardt</groupId>
<artifactId>robotframework-selenium2library-java</artifactId>
<version>1.4.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.robotframework</groupId>
<artifactId>robotframework-maven-plugin</artifactId>
<version>1.4.3</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<configuration>
<variables>
<variable>RESOURCES:${project.basedir}/resources</variable>
<variable>LIBRARIES:../common</variable>
<variable>LOGDIR:${logDir}</variable>
</variables>
<extraPathDirectories>
<extraPathDirectory>resources</extraPathDirectory>
<extraPathDirectory>src/libraries/custom</extraPathDirectory>
<extraPathDirectory>src/test/robotframework/acceptance/common</extraPathDirectory>
</extraPathDirectories>
<excludes>
<exclude>NotImplemented</exclude>
</excludes>
<nonCriticalTags>
<nonCriticalTag>BUG_OPENED</nonCriticalTag>
</nonCriticalTags>
<debugFile>${logDir}/robot_debug.log</debugFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Using classpath command I was able to run the testcases.
java -cp robotframework-2.8.6.jar org.robotframework.RobotFramework testcase

Only one of tofile and todir may be set

When I compile a project with the POM contents as given below with clean install post-site -Pdev, I get the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project test-copy: An Ant BuildException has occured:
Only one of tofile and todir may be set.
[ERROR] around Ant part ...<copy todir="temp/config" file="temp1/temp1.conf" tofile="temp2/temp.conf" failonerror="false">... # 5:98 in Projects/TestCopy/test-copy/target/antrun/build-main.xml
How do I fix this problem?
Ant script that got generated during compiling:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="maven-antrun-" default="main" >
<target name="main">
<echo>Base Dir:</echo>
<copy todir="temp/config" file="temp1/temp1.conf" tofile="temp2/temp.conf" failonerror="false">
<fileset file="/Users/Projects/TestCopy/test-copy/*.conf"/>
</copy>
</target>
</project>
POM Contents:
<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>temp.test.copy</groupId>
<artifactId>test-copy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-copy</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Base Dir1: </echo>
<copy todir="temp/config" failonerror="false">
<fileset file="${basedir}/*.conf"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>post-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Base Dir: </echo>
<copy file="temp1/temp1.conf"
tofile="temp2/temp.conf">
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
The problem is the missing id tags in your executions. Since you did not define an id, Maven will use default. Now you have a default execution in your base pom as well as one in your dev profile.
Since the id is identical, maven simply merges poth definitions, resulting in the rather weird mixed ant that you got.
So simply add to different <id> tags to bot executions and you are fine.
Additional note:
You should always included ids in your executions, in addition to preventing effects like your problem, they server als as documentation, because the id is printed out during the build. So choose something meaningful like: copy-configuration-dir and copy-single-config-to-site.

How to add multiple source directories to pom.xml

I am getting package org.testng.annotations does not exist error when I am trying to compile multiple source directories using maven. I am running webdriver tests and all of my tests are importing import org.testng.annotations. I am using IntelliJ 12 and my src directory looks like this -
src
-->main
--> java
--> package1
--> file1.java
--> package2
--> file2.java
-->test
--> java
--> package1
--> file1.java
--> file2.java
--> package2
--> package3
--> package4
--> package5
--> package6
and the build plugin I am using in the pom.xml looks like this -
<?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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>core-xxxxx</artifactId>
<groupId>core-xxxxx</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tests</artifactId>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>2.32.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
why am I getting the org.testng.annotations not found error?
The default layout for a maven project is the following:
src
main
java
test
java
within the src/main/java/ directory the package name for you productive java class source files should added. within the src/test/java/ the package name for the unit tests java class source files.
And you should NOT change the layout if you don't have really really good reasons to do so.
Furthermore redefining the defaults of Maven defaults (target, target/classes, target/test-classes etc.) is against the convention over configuration paradigm.
Be aware that you need to follow a naming convention for unit tests which means you unit tests should be named like the following:
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
But in your case I assume that we are talking about integration tests which means you need to use the following naming convention
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
Apart from that you should now that unit tests in Maven will be executed by the maven-surefire-plugin whereas the integration tests will be executed by the maven-failsafe-plugin.
To get around the testng annotations not found errors I ended up moving the base class from src/main/java/package1/baseclass.java to src/main/test/package1/baseclass.java and the testng annotations errors resolved

Converting a Maven 1 project into Maven 2

I'm new to Maven, and have been running into some difficulty in a project. I am to convert a Maven 1 project into Maven 2.
I started with these files:
maven.xml -- contains custom build scripts
project.properties -- general build settings
project.xml -- Project Object Model (POM) definition
From my understanding, Maven 2 project I must move these files into these:
pom.xml -- POM definition
(and possibly) settings.xml -- local configuration
I have gone about this by using the command 'mvn one:convert'.
This seemed to take care of project.xml > pom.xml
I then added a to pom.xml to include project.properties (which seemed to work).
Am I right in assuming that all I have left is to transfer over the contents of maven.xml >> pom.xml ?
maven.xml starts with:
<project default="site_deploy"
xmlns:ant="jelly:ant"
xmlns:maven="jelly:maven"
xmlns:j="jelly:core"
xmlns:util="jelly:util">
<ant:property environment="env"/>
and contains contains goals such as:
<goal name="site_deploy">
<attainGoal name="clean"/>
<attainGoal name="clean:clean"/>
<ant:delete dir="${maven.src.dir}/core/target" />
<attainGoal name="core_deploy"/>
</goal>
<goal name="core">
<maven:maven
descriptor="core/project.xml"
goals="jar:install"/>
<ant:property name="m2Dir" value="${maven.repo.local}/../../.m2/repository/app/${application.version}"/>
<ant:property name="m1Path" value="${maven.repo.local}/${application.id}/jars/${application.id}-core-${application.version}.jar"/>
<ant:echo message="copying jar m1 to m2 (${m1Path}) to (${m2Dir})" />
<ant:mkdir dir="${m2Dir}"/>
<ant:copy todir="${m2Dir}" file="${m1Path}" />
</goal>
From my reading if not bound to any build phase, goals can be executed outside of the build lifecycle by direct invocation, the second way being to write plugins for the goals.
How would I identify if the goals have dependencies-- how would I go about writing a plug-in? I've been referring mostly to the maven guides on apache.org, but some of it is hard to follow.
Here is the pom file generated:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>${application.id}</groupId>
<artifactId>${application.artifact}</artifactId>
<version>${application.version}</version>
<name>${application.name}</name>
<inceptionYear>2007</inceptionYear>
<organization>
<name>OrganizationName</name>
<url>http://organization.url</url>
</organization>
<scm>
<connection>scm:svn:connection</connection>
<url>http://svn.organization.local/svn/trunk/application_name</url>
</scm>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-changes-plugin</artifactId>
<configuration>
<xmlPath>${basedir}/xdocs/changes.xml</xmlPath>
</configuration>
</plugin>
</plugins>
</reporting>
</project>

Command to run only one specific test plugin defined in Maven

I have a Maven POM file with a plugin that runs on the test phase. What command line arguments do I have to pass mvn in order to execute just that plugin rather than all of the plugins for that phase? I am also trying to execute a specific ant-run plugin, that looks like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>com.googlecode.jslint4java</groupId>
<artifactId>jslint4java-ant</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>jslint</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant antfile="${basedir}/jslint.xml">
<property name="root" location="${basedir}" />
<target name="jslint" />
</ant>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Thanks.
Specify the fully-qualified goal in the form of:
mvn groupID:artifactID:version:goal
For example:
mvn sample.plugin:maven-hello-plugin:1.0-SNAPSHOT:sayhi
EDIT: I'm modifying my answer to cover the update of the initial question and a comment from the OP.
I won't cover all the details but, it the particular case of the antrun plugin, you could just run:
mvn antrun:run
But now that you've updated the question, I understand that things are a bit more complicated than what I thought initially and I don't think that this will actually work. I mean, invoking mvn antrun:run won't fail but it won't pick up the configuration of the execution bound to the test phase.
The only (ugly) solution I can think of would be to add another maven-antrun-plugin configuration in a specific profile, something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>com.googlecode.jslint4java</groupId>
<artifactId>jslint4java-ant</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<configuration>
<tasks>
<ant antfile="${basedir}/jslint.xml">
<property name="root" location="${basedir}" />
<target name="jslint" />
</ant>
</tasks>
</configuration>
</plugin>
And to use this profile when calling antrun:run:
mvn antrun:run -Pmyprofile-for-antrun