You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by MadVet <in...@yandex.ru> on 2008/03/03 12:50:38 UTC

Maven Ant plugin: fixed absolute links on related projects

Hi

I have found and fix one problem with Maven Ant plugin. I had worked with multi-module maven projects and found that Maven Ant plugin generate absolute path for referenced projects (another modules). Ant doesn't find them during compilation. So I have fix plugin - it calculates relative path to referenced projects.
Patch tested on Windows XP, Java 1.5.0_11.

maven-build.xml before fix:
  <!-- ====================================================================== -->
  <!-- Defining classpaths                                                    -->
  <!-- ====================================================================== -->

  <path id="build.classpath">
    <fileset dir="${maven.repo.local}">
      <include name="javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
      <include name="javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
<!-- other libs -->

      <include name="D:/dev/projects/trunk/src/Security/target/Security-1.0.0.21-SNAPSHOT.jar"/>
      <include name="D:/dev/projects/trunk/src/BusinessLogic/target/BusinessLogic-1.0.0.21-SNAPSHOT.jar"/>
<!-- other module references -->

    </fileset>
  </path>


maven-build.xml after fix:

  <!-- ====================================================================== -->
  <!-- Defining classpaths                                                    -->
  <!-- ====================================================================== -->

  <path id="build.classpath">
    <fileset dir="${maven.repo.local}">
      <include name="javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
      <include name="javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
      <include name="commons-lang/commons-lang/2.1/commons-lang-2.1.jar"/>
<!-- other libs -->
    </fileset>
    <pathelement location="../Security/target/Security-1.0.0.21-SNAPSHOT.jar"/>
    <pathelement location="../BusinessLogic/target/BusinessLogic-1.0.0.21-SNAPSHOT.jar"/>
<!-- other module references -->
  </path>

Svn diff:
Index: main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
===================================================================
--- main/java/org/apache/maven/plugin/ant/AntBuildWriter.java	(revision 628820)
+++ main/java/org/apache/maven/plugin/ant/AntBuildWriter.java	(working copy)
@@ -23,6 +23,7 @@
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
@@ -535,7 +536,44 @@
             return localRepository.getAbsolutePath();
         }
     }
+    
+    
+    private String buildRelativePath(File folder, File file)
+    {
+    	File parent = folder;
+    	String path = "";
+    	while ( parent != null && !isContainedIn( file, parent ) )
+    	{
+    		parent = parent.getParentFile();
+    		path += "../";
+    	}
+    	if (parent == null)
+    		return file.getAbsolutePath();
+    	
+    	return path + cutPathToFolder( file, parent );
+    }
+    
+    private boolean isContainedIn( File file, File folder )
+    {
+    	File parent = file.getParentFile();
+    	while ( parent != null && !parent.equals( folder ) )
+    		parent = parent.getParentFile();
+    	return parent != null;
+    }
+    
+    private String cutPathToFolder( File file, File folder )
+    {
+    	File parent = file.getParentFile();
+    	String path = file.getName();
+    	while ( parent != null && !parent.equals( folder ) )
+    	{
+    		path = parent.getName() + "/" + path;
+    		parent = parent.getParentFile();
+    	}
+    	return path;
+    }
 
+
     /**
      * Write path definition in the writer only for a non-POM project.
      *
@@ -550,18 +588,26 @@
 
         AntBuildWriterUtil.writeCommentText( writer, "Defining classpaths", 1 );
 
+        List notRepositoryArtifacts = new ArrayList();
+
         writer.startElement( "path" );
         writer.addAttribute( "id", "build.classpath" );
         writer.startElement( "fileset" );
         writer.addAttribute( "dir", "${maven.repo.local}" );
+        
         if ( !project.getCompileArtifacts().isEmpty() )
         {
             for ( Iterator i = project.getCompileArtifacts().iterator(); i.hasNext(); )
             {
                 Artifact artifact = (Artifact) i.next();
-                writer.startElement( "include" );
-                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
-                writer.endElement(); // include
+                if (isContainedIn(artifact.getFile(), localRepository))
+                {
+                    writer.startElement( "include" );
+                	writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
+                    writer.endElement(); // include
+                }
+                else
+                	notRepositoryArtifacts.add(artifact);
             }
         }
         else
@@ -571,8 +617,20 @@
             writer.endElement(); // include
         }
         writer.endElement(); // fileset
+        
+        for ( Iterator i = notRepositoryArtifacts.iterator(); i.hasNext(); )
+        {
+        	Artifact artifact = (Artifact) i.next();
+        	writer.startElement( "pathelement" );
+        	writer.addAttribute( "location", 
+        		buildRelativePath(project.getBasedir(), artifact.getFile()) );//artifact.getFile().getAbsolutePath() );
+        	writer.endElement();
+        }
+        
         writer.endElement(); // path
 
+        List notRepositoryTestArtifacts = new ArrayList();
+        
         writer.startElement( "path" );
         writer.addAttribute( "id", "build.test.classpath" );
         writer.startElement( "fileset" );
@@ -582,9 +640,14 @@
             for ( Iterator i = project.getTestArtifacts().iterator(); i.hasNext(); )
             {
                 Artifact artifact = (Artifact) i.next();
-                writer.startElement( "include" );
-                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
-                writer.endElement(); // include
+                if (isContainedIn(artifact.getFile(), localRepository))
+                {
+	                writer.startElement( "include" );
+	                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
+	                writer.endElement(); // include
+                }
+                else
+                	notRepositoryTestArtifacts.add(artifact);
             }
         }
         else
@@ -594,6 +657,15 @@
             writer.endElement(); // include
         }
         writer.endElement(); // fileset
+        
+        for ( Iterator i = notRepositoryTestArtifacts.iterator(); i.hasNext(); )
+        {
+        	Artifact artifact = (Artifact) i.next();
+        	writer.startElement( "pathelement" );
+        	writer.addAttribute( "location", buildRelativePath(project.getBasedir(), artifact.getFile()) );
+        	writer.endElement();
+        }
+        
         writer.endElement(); // path
 
         AntBuildWriterUtil.writeLineBreak( writer );


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Maven Ant plugin: fixed absolute links on related projects

Posted by Dennis Lundberg <de...@apache.org>.
Can you create an issue for this in JIRA:
   http://jira.codehaus.org/browse/MANT

There you can attach your patch. We use JIRA to track all fixes and 
enhancements.


Thank you!


MadVet wrote:
> Hi
> 
> I have found and fix one problem with Maven Ant plugin. I had worked with multi-module maven projects and found that Maven Ant plugin generate absolute path for referenced projects (another modules). Ant doesn't find them during compilation. So I have fix plugin - it calculates relative path to referenced projects.
> Patch tested on Windows XP, Java 1.5.0_11.
> 
> maven-build.xml before fix:
>   <!-- ====================================================================== -->
>   <!-- Defining classpaths                                                    -->
>   <!-- ====================================================================== -->
> 
>   <path id="build.classpath">
>     <fileset dir="${maven.repo.local}">
>       <include name="javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
>       <include name="javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
> <!-- other libs -->
> 
>       <include name="D:/dev/projects/trunk/src/Security/target/Security-1.0.0.21-SNAPSHOT.jar"/>
>       <include name="D:/dev/projects/trunk/src/BusinessLogic/target/BusinessLogic-1.0.0.21-SNAPSHOT.jar"/>
> <!-- other module references -->
> 
>     </fileset>
>   </path>
> 
> 
> maven-build.xml after fix:
> 
>   <!-- ====================================================================== -->
>   <!-- Defining classpaths                                                    -->
>   <!-- ====================================================================== -->
> 
>   <path id="build.classpath">
>     <fileset dir="${maven.repo.local}">
>       <include name="javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
>       <include name="javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
>       <include name="commons-lang/commons-lang/2.1/commons-lang-2.1.jar"/>
> <!-- other libs -->
>     </fileset>
>     <pathelement location="../Security/target/Security-1.0.0.21-SNAPSHOT.jar"/>
>     <pathelement location="../BusinessLogic/target/BusinessLogic-1.0.0.21-SNAPSHOT.jar"/>
> <!-- other module references -->
>   </path>
> 
> Svn diff:
> Index: main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
> ===================================================================
> --- main/java/org/apache/maven/plugin/ant/AntBuildWriter.java	(revision 628820)
> +++ main/java/org/apache/maven/plugin/ant/AntBuildWriter.java	(working copy)
> @@ -23,6 +23,7 @@
>  import java.io.FileOutputStream;
>  import java.io.FileWriter;
>  import java.io.IOException;
> +import java.util.ArrayList;
>  import java.util.Collections;
>  import java.util.Iterator;
>  import java.util.List;
> @@ -535,7 +536,44 @@
>              return localRepository.getAbsolutePath();
>          }
>      }
> +    
> +    
> +    private String buildRelativePath(File folder, File file)
> +    {
> +    	File parent = folder;
> +    	String path = "";
> +    	while ( parent != null && !isContainedIn( file, parent ) )
> +    	{
> +    		parent = parent.getParentFile();
> +    		path += "../";
> +    	}
> +    	if (parent == null)
> +    		return file.getAbsolutePath();
> +    	
> +    	return path + cutPathToFolder( file, parent );
> +    }
> +    
> +    private boolean isContainedIn( File file, File folder )
> +    {
> +    	File parent = file.getParentFile();
> +    	while ( parent != null && !parent.equals( folder ) )
> +    		parent = parent.getParentFile();
> +    	return parent != null;
> +    }
> +    
> +    private String cutPathToFolder( File file, File folder )
> +    {
> +    	File parent = file.getParentFile();
> +    	String path = file.getName();
> +    	while ( parent != null && !parent.equals( folder ) )
> +    	{
> +    		path = parent.getName() + "/" + path;
> +    		parent = parent.getParentFile();
> +    	}
> +    	return path;
> +    }
>  
> +
>      /**
>       * Write path definition in the writer only for a non-POM project.
>       *
> @@ -550,18 +588,26 @@
>  
>          AntBuildWriterUtil.writeCommentText( writer, "Defining classpaths", 1 );
>  
> +        List notRepositoryArtifacts = new ArrayList();
> +
>          writer.startElement( "path" );
>          writer.addAttribute( "id", "build.classpath" );
>          writer.startElement( "fileset" );
>          writer.addAttribute( "dir", "${maven.repo.local}" );
> +        
>          if ( !project.getCompileArtifacts().isEmpty() )
>          {
>              for ( Iterator i = project.getCompileArtifacts().iterator(); i.hasNext(); )
>              {
>                  Artifact artifact = (Artifact) i.next();
> -                writer.startElement( "include" );
> -                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
> -                writer.endElement(); // include
> +                if (isContainedIn(artifact.getFile(), localRepository))
> +                {
> +                    writer.startElement( "include" );
> +                	writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
> +                    writer.endElement(); // include
> +                }
> +                else
> +                	notRepositoryArtifacts.add(artifact);
>              }
>          }
>          else
> @@ -571,8 +617,20 @@
>              writer.endElement(); // include
>          }
>          writer.endElement(); // fileset
> +        
> +        for ( Iterator i = notRepositoryArtifacts.iterator(); i.hasNext(); )
> +        {
> +        	Artifact artifact = (Artifact) i.next();
> +        	writer.startElement( "pathelement" );
> +        	writer.addAttribute( "location", 
> +        		buildRelativePath(project.getBasedir(), artifact.getFile()) );//artifact.getFile().getAbsolutePath() );
> +        	writer.endElement();
> +        }
> +        
>          writer.endElement(); // path
>  
> +        List notRepositoryTestArtifacts = new ArrayList();
> +        
>          writer.startElement( "path" );
>          writer.addAttribute( "id", "build.test.classpath" );
>          writer.startElement( "fileset" );
> @@ -582,9 +640,14 @@
>              for ( Iterator i = project.getTestArtifacts().iterator(); i.hasNext(); )
>              {
>                  Artifact artifact = (Artifact) i.next();
> -                writer.startElement( "include" );
> -                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
> -                writer.endElement(); // include
> +                if (isContainedIn(artifact.getFile(), localRepository))
> +                {
> +	                writer.startElement( "include" );
> +	                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
> +	                writer.endElement(); // include
> +                }
> +                else
> +                	notRepositoryTestArtifacts.add(artifact);
>              }
>          }
>          else
> @@ -594,6 +657,15 @@
>              writer.endElement(); // include
>          }
>          writer.endElement(); // fileset
> +        
> +        for ( Iterator i = notRepositoryTestArtifacts.iterator(); i.hasNext(); )
> +        {
> +        	Artifact artifact = (Artifact) i.next();
> +        	writer.startElement( "pathelement" );
> +        	writer.addAttribute( "location", buildRelativePath(project.getBasedir(), artifact.getFile()) );
> +        	writer.endElement();
> +        }
> +        
>          writer.endElement(); // path
>  
>          AntBuildWriterUtil.writeLineBreak( writer );
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
> 
> 


-- 
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org