Section 2.4. Defining a preGoal


2.4. Defining a preGoal

Since you would like to incorporate the generated SOAP client into your own project, you need to use the Maven Axis plug-in. When the Axis plug-in generates source, your build needs to make sure that the source it generates is included in your project's compilation. To accomplish this, you need to define a preGoal and postGoal on the java:compile goal.

2.4.1. How do I do that?

The Axis plug-in generates source files and places them in maven.axis.generated.dir, which defaults to target/axis/src. While you don't want to copy the generated source files to your project by copying them to src/main/java, you do want these files to be included in your project's artifact. You need to execute the axis:compile goal before you execute the java:compile goal, as axis:compile will add the source generated by axis:wsdl2java to the compilation source path. The following maven.xml file uses a preGoal which executes another goal before java:compile:


Note: Maven 2 gets rid of preGoal and postGoal. In Maven 2 a build is defined as a series of phases, such as build, compile, and test. Specific goals bind to phases in a project's build lifecycle.
<?xml version="1.0" encoding="UTF-8"?>    <project default="jar"     xmlns:j="jelly:core"      xmlns:ant="jelly:ant"      xmlns:maven="jelly:maven"    <preGoal name="java:compile">   <attainGoal name="axis:compile" /> </preGoal> </project>

Executing the java:compile goal produces the following output (some file paths have been truncated):

 __  __ |  \/  |_ _ _Apache_ _ _ _ _ | |\/| / _` \ V / -_) ' \  ~ intelligent projects ~ |_|  |_\_ _,_|\_/\_ _ _|_||_|  v. 1.0.2    build:start:    java:prepare-filesystem:     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\classes Invoking Axis plugin    java:compile: axis:prepare-filesystem:     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\axis\src     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\axis\build     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\axis\test    test:prepare-filesystem:     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\test-classes     [mkdir] Created dir: C:\dev\mavenbook\code\weather\target\test-reports find all .wsdl files in directory c:\dev\mavenbook\code\weather/src/wsdl generate .java files from C:\dev\mavenbook\code\weather\src\wsdl\weather.wsdl    test:test-resources:    java:jar-resources:    axis:axis:     [axis-wsdl2java] WSDL2Java C:\dev\mavenbook\code\weather\src\wsdl\weather.wsdl Parsing XML file:  C:\dev\mavenbook\code\weather\src\wsdl\weather.wsdl Generating C:\...\gov\weather\forecasts\xml\DWMLgen\schema\ndfdXML_xsd\FormatType.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\schema\ndfdXML_xsd\ProductType.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\schema\ndfdXML_xsd\ WeatherParametersType.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\wsdl\ndfdXML_wsdl\NdfdXMLPortType.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\wsdl\ndfdXML_wsdl\ NdfdXMLBindingStub.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\wsdl\ndfdXML_wsdl\NdfdXML.java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\wsdl\ndfdXML_wsdl\NdfdXMLLocator. java Generating C:\...\gov\weather\forecasts\xml\DWMLgen\wsdl\ndfdXML_wsdl\NdfdXMLTestCase. java move the generated testcases to folder c:\dev\mavenbook\code\weather/target/axis/test     [move] Moving 1 files to C:\dev\mavenbook\code\weather\target\axis\test    axis:copy:     [copy] Copying 7 files to C:\dev\mavenbook\code\weather\target\axis\build adding c:\dev\mavenbook\code\weather/target/axis/src to the maven.compile.src.set    axis:compile:     [echo] Compiling to c:\dev\mavenbook\code\weather/target/classes    java:compile:     [javac] Compiling 8 source files to C:\dev\mavenbook\code\weather\target\classes Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. BUILD SUCCESSFUL Total time: 7 seconds


Note: The axis:compile goal is misnamed and produces misleading output. While you may think it actually compiles the generated client files, it is simply adding target/axis/src to the compilation source path.

When you execute java:compile, Maven will automatically execute axis:compile, which in turn depends on a number of other goals in the Axis plug-in which generate source from all WSDL documents contained in src/wsdl. These generated classes will then be automatically added to the compilation. Since some of the source for this project is generated from WSDL, it would be redundant to store both the WSDL and the generated source in version control. Inserting axis:compile into the mix with a preGoal on java:compile includes these classes in the compilation without requiring you to copy them to src/main/java.

2.4.2. What just happened?

The Axis WSDL2Java tool generates classes in the target/axis/src directory. Because you don't plan on modifying the generated classes, you have defined a preGoal on java:compile which executes axis:compile. axis:compile adds maven.axis.generated.dir to the maven.compile.src.set property, which means the generated classes will be compiled when java:compile is executed. In other words, axis:compile tells Maven to include target/axis/src as a directory which contains source code to be compiled and packaged in any project artifact.


Note: A postGoal uses the same syntax as a preGoal, and defines a block of Jelly script to be executed before the goal specified in the name attribute.

Okay, so what exactly did this axis:compile goal do again? Here is the goal definition from this plug-in's plugin.jelly file, which you can see in ~/.maven/cache/maven-axis-plugin-0.7/plugin.jelly:

  <goal name="axis:compile"       description="Compile the generated .java files."       prereqs="axis:copy">        <j:if test="${wsdlPresent =  = 'true'}">       <ant:path >         <ant:pathelement location="${maven.axis.build.dir}"/>       </ant:path>          <log:info>adding ${maven.axis.generated.dir}                  to the maven.compile.src.set</log:info>       <maven:addPath  ref/>     </j:if>   </goal>

The operative line has been highlighted: the axis.src.set path is created and subsequently added to maven.compile.src.set. This means the generated Axis classes will now be included in the compile performed by java:compile.



Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net