<warning-long-email/>

This patch includes the following changes:

1. Support for adding arbitrary callbacks to a build that follows a 
   similar pattern that Jason did for preparing Java source.

2. Changes to build-j2ee.xml:

2a: use the new generic callback mechanism
2b: use the jars specified by the project dependencies
2c: use the nested <manifest> element of the <war> task the way the 
    current <jar> targets do
2d: create the resulting .war file in the same place as the .jar goes

3. Fix to build-maven.xml to use the correct ${maven.organization} 
   property so the manifest has the correct organization name.

Generic Callbacks
=================

Jason introduced the concept of a "callback" into a non-maven build file
for the purposes of giving the Maven user a way to do "something" during
the build process.  Specifically, he introduced a way to prepare source
code before the actual compilation. I (amongst others I'm sure) *really*
liked this, and decided to make it easy to add callbacks into a build.

A callback depends on two properties that have to be set by the end user:

maven.callback.<callbackName>.buildFile
maven.callback.<callbackName>.buildTarget

These are (obviously) the build file and target to call using the <ant>
task.  If either of these attributes are not set, then the callback will
not be "fired."

The callback target will automatically be passed all of the current
properties, but it won't be passed references due to a limitation in Ant
1.4.1.  The "classpath" path (which holds all of a project's dependencies)
is passed to the target as the property "maven.build.classpath", which can
be used like

<some_task_that_needs_a_classpath>
  <classpath path="${maven.build.classpath}" />
</some_task_that_needs_a_classpath>

Adding a Generic Callback
=========================

To add a callback into a build-*.xml, you need to do the following:

1) Insert the following lines at the top of your build-*.xml file:

#set( $callbackCategory = "j2ee" )
#parse("build.init.target")
#set( $callbackCategory = "" )

(where j2ee is your "category" name)

2) In your target's depends attribute, do the following to add the 
   callback (example taken from build-j2ee.xml):

   <target name="war"
        depends="local-init,
                 war-error,
                 war-init,
                 #callback("preprocess-war"),
                 war-build,
                 #callback("postprocess-war")"
        description="o Create a war file" />

3) In your Control-*.vm file, adding the following:

callbacks.put( "j2ee", [ "preprocess-war", "postprocess-war" ] );

(where j2ee is your "cateory", and the array are the callback targets you 
want to add to your build).

What the Callbacks look like
============================

In your final build-*.xml the targets look like the following (taken from 
build-j2ee.xml) :

  <target name="preprocess-war-callback-check">
    <condition property="maven.callback.preprocess-war.ok">
      <not>
        <or>
          <equals arg1="${maven.callback.preprocess-war.buildFile}"
                  arg2="$${maven.callback.preprocess-war.buildFile}" />
          <equals arg1="${maven.callback.preprocess-war.buildTarget}"
                  arg2="$${maven.callback.preprocess-war.buildTarget}" />
        </or>
      </not>
    </condition>
  </target>

  <target name="preprocess-war-callback"
          depends="preprocess-war-callback-check"
          if="maven.callback.preprocess-war.ok">

    <ant antfile="${maven.callback.preprocess-war.buildFile}"
         target="${maven.callback.preprocess-war.buildTarget}">
      <property name="maven.build.classpath" value="classpath" />
    </ant>

  </target>

And the target that depends on these callback targets looks like the 
following (taken from build-j2ee.xml) :

    <target name="war"
        depends="local-init,
                 war-error,
                 war-init,
                 preprocess-war-callback,
                 war-build,
                 postprocess-war-callback"
        description="o Create a war file" />


Glenn McAllister
SOMA Networks, Inc.

Index: src/java/org/apache/maven/BaseProjectTask.java
===================================================================
RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/BaseProjectTask.java,v
retrieving revision 1.8
diff -u -r1.8 BaseProjectTask.java
--- src/java/org/apache/maven/BaseProjectTask.java      1 May 2002 16:54:31 -0000      
 1.8
+++ src/java/org/apache/maven/BaseProjectTask.java      1 May 2002 21:48:29 -0000
@@ -140,6 +140,7 @@
             context.put("delegators", new HashMap());
             context.put("project", mavenProject);
             context.put("jars", ListTask.getList(listFile));
+            context.put("callbacks", new HashMap());
             return context;
         }
         catch (Exception e)
Index: src/java/org/apache/maven/ProjectProperties.java
===================================================================
RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/ProjectProperties.java,v
retrieving revision 1.29
diff -u -r1.29 ProjectProperties.java
--- src/java/org/apache/maven/ProjectProperties.java    25 Apr 2002 15:51:27 -0000     
 1.29
+++ src/java/org/apache/maven/ProjectProperties.java    1 May 2002 21:48:30 -0000
@@ -235,16 +235,21 @@
     private void createClasspathReference()
     {
         Path classpath = new Path(getProject());
+        PatternSet psClasspath = new PatternSet();
 
         for (Iterator i = getMavenProject().getDependencies().iterator(); 
i.hasNext();)
         {
             Dependency dependency = (Dependency) i.next();
+            
             Path p = new Path(getProject());
             p.setPath(new File(libRepo, dependency.getJar()).getAbsolutePath());
+            
             classpath.append(p);
+            psClasspath.createInclude().setName(dependency.getJar());
         }
 
         getProject().addReference("classpath", classpath);
+        getProject().addReference("classpath.patternset", psClasspath);
     }
 
     /**
Index: src/templates/build/Control-j2ee.vm
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/Control-j2ee.vm,v
retrieving revision 1.1
diff -u -r1.1 Control-j2ee.vm
--- src/templates/build/Control-j2ee.vm 1 May 2002 16:54:31 -0000       1.1
+++ src/templates/build/Control-j2ee.vm 1 May 2002 21:48:30 -0000
@@ -12,3 +12,9 @@
 ## Make the list of build-j2ee.xml delegators
 ## -------------------------------------------------------
 $delegators.put("war","build-j2ee.xml")
+
+
+## -------------------------------------------------------
+## Make the list of build-j2ee.xml callbacks
+## -------------------------------------------------------
+$callbacks.put( "j2ee", [ "preprocess-war", "postprocess-war" ] );
Index: src/templates/build/Control.vm
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/Control.vm,v
retrieving revision 1.17
diff -u -r1.17 Control.vm
--- src/templates/build/Control.vm      1 May 2002 16:54:31 -0000       1.17
+++ src/templates/build/Control.vm      1 May 2002 21:48:30 -0000
@@ -1,3 +1,11 @@
+
+## -------------------------------------------------------
+## Macro for adding a callback target name in a build file
+## -------------------------------------------------------
+
+#macro( callback $cbName )
+${cbName}-callback#end
+
 ## -------------------------------------------------------
 ## Make our list of build system elements
 ## -------------------------------------------------------
Index: src/templates/build/build-j2ee.xml
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/build-j2ee.xml,v
retrieving revision 1.3
diff -u -r1.3 build-j2ee.xml
--- src/templates/build/build-j2ee.xml  1 May 2002 15:47:58 -0000       1.3
+++ src/templates/build/build-j2ee.xml  1 May 2002 21:48:30 -0000
@@ -2,7 +2,9 @@
 
 <project name="$project.id" default="war" basedir="$antBasedir">
 
+#set( $callbackCategory = "j2ee" )
 #parse("build.init.target")
+#set( $callbackCategory = "" )
 
     <!-- ================================================================== -->
     <!-- L O C A L   I N I T                                                -->
@@ -11,106 +13,88 @@
     <target name="local-init" depends="init">
         <!-- Pick up tool specific defaults -->
         <property file="${maven.home}/j2ee.properties"/>
+        
         <!-- Check all requirements are available -->
         <condition property="maven.war.ready">
             <and>
+                <not>
+                    <equals arg1="${maven.war.name}"
+                            arg2="$${maven.war.name}" />
+                </not>
                 <available file="${maven.war.src}" type="dir"/>
                 <available file="${maven.war.content}" type="dir"/>
                 <available file="${maven.war.webxml}" type="file"/>
             </and>
         </condition>
-        <!-- set maven.war.preprocessing if the user has set both properties,
-             maven.war.preprocessBuildFile and maven.war.preprocessTarget
-         -->
-        <condition property="maven.war.preprocessing">
-            <and>
-                <not>
-                    <or>
-                        <equals arg1="${maven.war.preprocessBuildFile}"
-                                arg2="${maven.war.preprocessBuildFile}"/>
-                        <equals arg1="${maven.war.preprocessTarget}"
-                                arg2="${maven.war.preprocessTarget}"/>
-                        <equals arg1="${maven.war.ready}"
-                                arg2="${maven.war.ready}"/>
-                    </or>
-                </not>
-            </and>
-        </condition>
-        <!-- set maven.war.postprocessing if the user has set both properties,
-             maven.war.postprocessBuildFile and maven.war.postprocessTarget
-         -->
-        <condition property="maven.war.postprocessing">
-            <not>
-                <or>
-                    <equals arg1="${maven.war.postprocessBuildFile}"
-                            arg2="${maven.war.postprocessBuildFile}"/>
-                    <equals arg1="${maven.war.postprocessTarget}"
-                            arg2="${maven.war.postprocessTarget}"/>
-                    <equals arg1="${maven.war.ready}"
-                            arg2="${maven.war.ready}"/>
-                </or>
-            </not>
-        </condition>
-
     </target>
   
     <target name="war"
-        
depends="local-init,war-error,war-init,war-preprocess,war-build,war-postprocess"
+        depends="local-init,
+                 war-error,
+                 war-init,
+                 #callback("preprocess-war"),
+                 war-build,
+                 #callback("postprocess-war")"
         description="o Create a war file" />
-       
+ 
+    <target name="war-error" unless="maven.war.ready">
+        <!-- extend this list with requirements as needed -->
+        <fail>
+    +------------------------------------------------------------------------------
+    |
+    | ERROR: One of the required properties for maven:war was not provided.
+    |
+    |        Please ensure that the property maven.war.name is defined:
+    |
+    |        maven.war.name = ${maven.war.name}
+    |
+    |        Please ensure that the following directories exist:
+    |
+    |        ${maven.war.src}
+    |        ${maven.war.content}
+    |
+    |        and the following files:
+    |
+    |        ${maven.war.webxml}
+    +------------------------------------------------------------------------------
+        </fail>
+    </target>
+      
     <target name="war-init" if="maven.war.ready">
-        <!-- Create build directories -->
-        <mkdir dir="${maven.build.war}/${maven.war.name}"/>
-        <mkdir dir="${maven.build.war}/${maven.war.name}/META-INF"/>
-        <mkdir dir="${maven.build.war}/${maven.war.name}/WEB-INF"/>
-        <mkdir dir="${maven.build.war}/${maven.war.name}/WEB-INF/classes"/>
-        <mkdir dir="${maven.build.war}/${maven.war.name}/WEB-INF/lib"/>
+        <!-- nothing to do as of yet -->
     </target>    
     
     <target name="war-build" if="maven.war.ready">
+        <!-- make sure the source is compiled -->
         <ant antfile="${maven.home}/build-maven.xml" target="compile" />
+
+        <!-- setup any required resources -->
         <ant antfile="${maven.home}/build-maven.xml" target="jar-resources" />
-        <echo>building for ${maven.war.name}</echo>
-        <war warfile="${maven.build.war}/${maven.war.name}.war"
+        
+        <echo>Building WAR ${maven.war.name}</echo>
+        
+        <war warfile="${build.dir}/${maven.war.name}.war"
              webxml="${maven.war.webxml}">
             <fileset dir="${maven.war.content}"/>
-            <!--<fileset dir="${maven.war.templates}"/> -->
             <lib dir="${lib.repo}">
-                <include name="${maven.war.lib.includes}"/>
-                <exclude name="${maven.war.lib.excludes}"/>
+                <patternset refid="classpath.patternset" />
             </lib>
             <classes dir="${build.dest}">
                 <include name="${maven.war.classes.includes}" />
                 <exclude name="${maven.war.classes.excludes}" />
             </classes>
+            <manifest>
+                <attribute name="Built-By" value="${user.name}" />
+                <section name="${package}">
+                    <attribute name="Specification-Title" value="${id}" />
+                    <attribute name="Specification-Version" value="${currentVersion}" 
+/>
+                    <attribute name="Specification-Vendor" 
+value="${maven.organization}" />
+                    <attribute name="Implementation-Title" value="${package}" />
+                    <attribute name="Implementation-Version" 
+value="${currentVersion}" />
+                    <attribute name="Implementation-Vendor" 
+value="${maven.organization}" />
+                </section>
+            </manifest>
          </war>
     </target>
     
-    <target name="war-error" unless="maven.war.ready">
-        <!-- extend this list with requirements as needed -->
-        <echo>
-        One of the required properties for maven:war was not provided.
-        Please ensure that the following directories exist:
-        ${maven.war.src}
-        ${maven.war.content}
-        ------------------------------------------------------------------------
-        and the following files:
-        ${maven.war.webxml}
-        ------------------------------------------------------------------------
-        </echo>
-    </target>
-    
-    <target name="war-preprocess" if="maven.war.preprocessing">
-        <echo>pre processing for ${maven.war.name}</echo>
-        <ant antfile="${maven.war.preprocessBuildFile}"
-             target="${maven.war.preprocessBuildFile}" />
-    </target>
-
-    <target name="war-postprocess" if="maven.war.postprocessing">
-        <echo>post processing for ${maven.war.name}</echo>
-        <ant antfile="${maven.war.postprocessBuildFile}"
-             target="${maven.war.postprocessTarget}" />
-    </target>
-
-    
-</project>
\ No newline at end of file
+</project>
Index: src/templates/build/build-maven.xml
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/build-maven.xml,v
retrieving revision 1.40
diff -u -r1.40 build-maven.xml
--- src/templates/build/build-maven.xml 29 Apr 2002 20:03:56 -0000      1.40
+++ src/templates/build/build-maven.xml 1 May 2002 21:48:30 -0000
@@ -376,10 +376,10 @@
         <section name="${package}">
           <attribute name="Specification-Title" value="${id}"/>
           <attribute name="Specification-Version" value="${currentVersion}"/>
-          <attribute name="Specification-Vendor" value="${organization}"/>
+          <attribute name="Specification-Vendor" value="${maven.organization}"/>
           <attribute name="Implementation-Title" value="${package}"/>
           <attribute name="Implementation-Version" value="${currentVersion}"/>
-          <attribute name="Implementation-Vendor" value="${organization}"/>
+          <attribute name="Implementation-Vendor" value="${maven.organization}"/>
         </section>
       </manifest>
     </jar>
Index: src/templates/build/build.init.target
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/build.init.target,v
retrieving revision 1.25
diff -u -r1.25 build.init.target
--- src/templates/build/build.init.target       1 May 2002 01:34:46 -0000       1.25
+++ src/templates/build/build.init.target       1 May 2002 21:48:30 -0000
@@ -322,3 +322,43 @@
     <delete file="project.xml.${toVersion}"/>
     
   </target>
+
+  #if ($callbacks.get( $callbackCategory ))
+  #set( $cbPrefix = "${maven" )
+  #set( $cbFile = "buildFile}" )
+  #set( $cbTarget = "buildTarget}" )
+    
+  <!-- ================================================================== -->
+  <!-- C A L L B A C K S                                                  -->
+  <!-- ================================================================== -->
+  #foreach( $cbName in $callbacks.get( $callbackCategory ) )
+  #set( $buildFile = "${cbPrefix}.callback.${cbName}.${cbFile}" )
+  #set( $buildTarget = "${cbPrefix}.callback.${cbName}.${cbTarget}" )
+  
+  <target name="${cbName}-callback-check">
+    <condition property="maven.callback.${cbName}.ok">
+      <not>
+        <or>
+          <equals arg1="$buildFile"
+                  arg2="$$buildFile" />
+          <equals arg1="$buildTarget"
+                  arg2="$$buildTarget" />
+        </or>
+      </not>
+    </condition>
+  </target>
+  
+  <target name="#callback( $cbName )"
+          depends="${cbName}-callback-check"
+          if="maven.callback.${cbName}.ok">
+
+    <ant antfile="$buildFile"
+         target="$buildTarget">
+      <property name="maven.build.classpath" value="classpath" />
+    </ant>
+
+  </target>
+  
+  #end
+  #end
+  
Index: src/templates/build/j2ee.properties
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/templates/build/j2ee.properties,v
retrieving revision 1.3
diff -u -r1.3 j2ee.properties
--- src/templates/build/j2ee.properties 1 May 2002 15:47:22 -0000       1.3
+++ src/templates/build/j2ee.properties 1 May 2002 21:48:30 -0000
@@ -3,8 +3,8 @@
 maven.war.src=${src.dir}/webapps
 maven.war.content=${maven.war.src}/${maven.war.name}
 # classes are relative to build.dir
-maven.war.classes.includes=**/*.class
-maven.war.classes.excludes=
+maven.war.classes.includes=**
+maven.war.classes.excludes=**/package.html
 #jars are relative to lib.repo
 maven.war.lib.includes=*.jar
 maven.war.lib.excludes=
@@ -13,4 +13,4 @@
 #maven.war.prepareBuildFile=build.xml
 #maven.war.prepareBuildTarget=maven:war:preProcess
 #maven.war.updateBuildFile=build.xml
-#maven.war.updateBuildTarget=maven:war:postProcess
\ No newline at end of file
+#maven.war.updateBuildTarget=maven:war:postProcess

Reply via email to