2012-03-13 46 views
1

我使用maven-dependency-plugin:build-classpath来构建类路径文件。为了支持遗留部署,除了通常的一组依赖JAR之外,我还需要这个文件包含我正在构建的工件。在Maven中,我如何生成一个包含我正在构建的工件的类路径文件?

当前classpath的文件:

dep1.jar:dep2.jar 

类路径的文件我想:

project-I'm-building.jar:dep1.jar:dep2.jar 

我使用maven-antrun-插件生成包含类路径的构件JAR文件考虑,然后使用build-classpath选项添加依赖关系JAR。这看起来虽然不起眼。有没有更好的办法?

回答

1

这个工作对我来说:

<plugin> 
    <groupId>org.codehaus.gmaven</groupId> 
    <artifactId>gmaven-plugin</artifactId> 
    <executions> 
     <execution> 
      <id>build-classpath-files-for-artifact-and-direct-aspect-dependencies</id> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>execute</goal> 
      </goals> 
      <configuration> 
       <properties> 
        <outputPath>${path.classpath}</outputPath> 
        <prefix>${prefix.classpath}</prefix> 
       </properties> 
       <source><![CDATA[ 
// Function for keying artifacts (groupId:artifactId) 
def artId(art) {"${art.groupId}:${art.artifactId}".toString()}         

if (project.packaging != "tgz") { 
    log.info "Skipping generation of classpath file(s) as this isn't a tgz project" 
} else { 
    new File(project.properties.outputPath).mkdirs() 

    // Map artifact keys to versions (as resolved by this -dist project) 
    def artVers = project.runtimeArtifacts.collectEntries{[(artId(it)): it.version]} 

    // Get global Maven ProjectBuilder, used for resolving artifacts to projects 
    def builder = session.lookup('org.apache.maven.project.ProjectBuilder'); 

    // Build the classpath files, including both the dependencies plus the project artifact itself 
    (project.dependencyArtifacts.findAll{dep -> dep.type == 'jar' && dep.groupId == project.groupId} + project.artifact).each{art -> 
     def req = session.projectBuildingRequest.setResolveDependencies(true) 
     def depProj = builder.build(art, req).getProject(); 

     // Only include artifacts of type jar, and for which a resolved version exists (this excludes -dist artifacts) 
     def classpath = ([art] + depProj.runtimeArtifacts).findAll{a -> a.type == 'jar' && artVers[artId(a)] != null}.collect{ 
      "${project.properties.prefix}/${it.artifactId}-${artVers[artId(it)]}.jar" 
     }         
     def file = new File(project.properties.outputPath, art.artifactId + ".classpath") 
     log.info "Writing classpath with ${classpath.size} artifact(s) to " + file 
     file.write(classpath.join(":")) 
    } 
}                 
        ]]></source> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
相关问题