【问题标题】:In Maven how can I generate a classpath file that includes the artifact I'm building?在 Maven 中,如何生成包含我正在构建的工件的类路径文件?
【发布时间】:2012-03-13 17:09:03
【问题描述】:

我正在使用maven-dependency-plugin:build-classpath 来构建一个类路径文件。为了支持遗留部署,我需要这个文件来包含我正在构建的工件,以及通常的依赖 JAR 集。

当前类路径文件:

dep1.jar:dep2.jar

我想要的类路径文件:

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

我正在考虑使用 maven-antrun-plugin 生成一个包含工件 JAR 的类路径的文件,然后使用 build-classpath 选项添加依赖项 JAR。但这似乎并不优雅。有没有更好的办法?

【问题讨论】:

    标签: maven maven-dependency-plugin maven-ant-tasks


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多