Maven:如何使用 build.gradle 文件?

Posted

技术标签:

【中文标题】Maven:如何使用 build.gradle 文件?【英文标题】:Maven: How to use a build.gradle file? 【发布时间】:2019-03-27 23:43:41 【问题描述】:

我是 Maven 和 Gradle 的完整初学者。我有一个工作的 Maven 项目。现在我想从另一个使用 Gradle 构建的项目中添加一堆类。我想从 Gradle 项目中获取课程并在我的 Maven 项目中使用它。当我将这些类复制到我的 Maven 项目中时,我得到了一堆依赖错误。为了解决这些错误,我想将所需的内容添加到我的 pom.xml 文件中。

基本上,我想将build.gradle 文件中指定的所有依赖项添加到我的pom.xml 文件中。

pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myGroupId</groupId>
  <artifactId>myArtifactId</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>10</release>
        </configuration>
      </plugin>
    </plugins>  

  </build>

  <dependencies>

    <!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-core -->
    <dependency>
        <groupId>org.jgrapht</groupId>
        <artifactId>jgrapht-core</artifactId>
        <version>1.2.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-jdk1.5 -->
    <dependency>
        <groupId>org.jgrapht</groupId>
        <artifactId>jgrapht-jdk1.5</artifactId>
        <version>0.7.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-ext -->
    <dependency>
        <groupId>org.jgrapht</groupId>
        <artifactId>jgrapht-ext</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>27.0-jre</version>
      <!-- or, for android: -->
      <!--  <version>27.0-android</version>  -->
    </dependency>

    <!-- https://mvnrepository.com/artifact/net.sf.trove4j/trove4j -->
    <dependency>
        <groupId>net.sf.trove4j</groupId>
        <artifactId>trove4j</artifactId>
        <version>3.0.3</version>
    </dependency>



  </dependencies>

</project>

build.gradle 文件:

buildscript 
    repositories 
        jcenter()
        maven  url 'https://maven.rapidminer.com/content/groups/public/' 
       



plugins  id 'com.rapidminer.extension' version '0.8.2' 

// Define Maven artifact repositories
repositories 
    jcenter()
    maven  url 'https://maven.rapidminer.com/content/groups/public/' 
    ivy 
        url "https://github.com/rapidprom/rapidprom-libraries/raw/$project.properties["version"]/prom/"
        layout "pattern", 
            artifact "[module]-[revision]/[module]-[revision].[ext]"
            artifact "[module]-[revision]/[artifact].[ext]"
            ivy "[module]-[revision]/ivy-[module]-[revision].xml"
        
    
    ivy 
        url "https://github.com/rapidprom/rapidprom-libraries/raw/$project.properties["version"]/thirdparty/lib/"
        layout "pattern", 
            artifact "[module]-[revision]/[module]-[revision].[ext]"
            ivy "[module]-[revision]/ivy-[module]-[revision].xml"
        
    
    ivy 
        url "https://github.com/rapidprom/rapidprom-libraries/raw/$project.properties["version"]/thirdparty/resource/"
        layout "pattern", 
            artifact "[module]-[revision]/[module]-[revision].[ext]"
            ivy "[module]-[revision]/ivy-[module]-[revision].xml"
        
       


extensionConfig 

    // The extension name
    name 'RapidProM'

    /*
     * The artifact group which will be used when publishing the extensions Jar
     * and for package customization when initializing the project repository.
     *
     */
    groupId = 'org.rapidprom'

    /*
     * The extension vendor which will be displayed in the extensions about box
     * and for customizing the license headers when initializing the project repository.
     *
     */
    vendor = "Eindhoven University of Technology"

    /*
     * The vendor homepage which will be displayed in the extensions about box
     * and for customizing the license headers when initializing the project repository.
     *
     */
    homepage = "www.rapidprom.org"

    // enable shadowJar before rapidminer dependency (otherwise build fails)
    shadowJar 
        zip64 true
    

    // define RapidMiner version and extension dependencies
    dependencies 
        rapidminer '7.2.0'
        //extension namespace: 'text', version: '7.2.0'
    


// Define third party library dependencies
dependencies 

    compile group:"org.rapidprom", name:"ProM-Framework", version:"31245"
    compile "org.rapidprom:AcceptingPetriNet:6.7.93"
    compile "org.rapidprom:AlphaMiner:6.5.47"
    compile "org.rapidprom:Animation:6.5.50"
    compile "org.rapidprom:ApacheUtils:6.7.88"
    compile "org.rapidprom:BasicUtils:6.7.104"
    compile "org.rapidprom:BPMN:6.5.56"
    compile "org.rapidprom:BPMNConversions:6.5.48"
    compile "org.rapidprom:CPNet:6.5.84"
    compile "org.rapidprom:DataAwareReplayer:6.7.563"
    compile "org.rapidprom:DataPetriNets:6.5.291"
    compile "org.rapidprom:DottedChart:6.5.17"
    compile "org.rapidprom:EvolutionaryTreeMiner:6.7.154"
    compile "org.rapidprom:EventStream:6.5.72"
    compile "org.rapidprom:FeaturePrediction:6.5.61"
    compile "org.rapidprom:Fuzzy:6.5.33"
    compile "org.rapidprom:GraphViz:6.7.185"
    compile "org.rapidprom:GuideTreeMiner:6.5.18"
    compile "org.rapidprom:HeuristicsMiner:6.5.49"
    compile "org.rapidprom:HybridILPMiner:6.7.116"
    compile "org.rapidprom:InductiveMiner:6.7.297"
    compile "org.rapidprom:InductiveVisualMiner:6.7.413"
    compile "org.rapidprom:Log:6.7.293"
    compile "org.rapidprom:LogDialog:6.5.42"
    compile "org.rapidprom:LogProjection:6.5.38"
    compile "org.rapidprom:ModelRepair:6.5.12"
    compile "org.rapidprom:Murata:6.5.54"
    compile "org.rapidprom:PetriNets:6.7.95"
    compile "org.rapidprom:PNetAlignmentAnalysis:6.5.35"
    compile "org.rapidprom:PNAnalysis:6.7.122"
    compile "org.rapidprom:PNetReplayer:6.7.99"
    compile "org.rapidprom:PTConversions:6.5.3"
    compile "org.rapidprom:PomPomView:6.5.36"
    compile "org.rapidprom:SocialNetwork:6.5.35"
    compile "org.rapidprom:StreamAlphaMiner:6.5.15"
    compile "org.rapidprom:StreamAnalysis:6.5.38"
    compile "org.rapidprom:StreamInductiveMiner:6.5.42"
    compile "org.rapidprom:TransitionSystems:6.7.71"
    compile "org.rapidprom:TSPetriNet:6.5.35"
    compile "org.rapidprom:Uma:6.5.46"
    compile "org.rapidprom:Woflan:6.7.59"
    compile "org.rapidprom:XESLite:6.7.217"
    compile "org.rapidprom:Weka:6.7.3"

【问题讨论】:

您是否需要同时构建所有内容(maven 调用 gradle)或者您可以构建 gradle 库并将其安装到 maven,然后使用这些库构建基于 maven 的项目? @the_storyteller 后者。我不需要 Gradle 项目,我只想用 Maven 构建和安装 Gradle 库。 【参考方案1】:

要部署到 Maven,请使用 maven 插件。

apply plugin: 'maven'

group = 'com.company'
version = '1.0.0.6'
// To build and push development snapshots, add a suffix to the name
// version = '1.0.0.6-SNAPSHOT'

artifacts 
    archives jar


uploadArchives 
    repositories 
        mavenDeployer 
            snapshotRepository(url: 'https://maven.repo/bla') 
                authentication(userName: snapshotUser, password: snapshotPassword)
            
            repository(url: 'https://maven.repo/bla') 
                authentication(userName: releaseUser, password: releasePassword);
            
        
    

您既可以通过运行uploadArchives 任务安装到远程存储库(上面的示例是https://maven.repo/bla),也可以使用install 安装到maven 本地~/.m2/repository任务。

gradle install uploadArchives

如果您选择安装到本地 maven,您可以使用 dependencies mavenLocal() 或将其包含在您的 pom.xml 中,将对本地 maven 的依赖添加到其他 gradle 项目中

<settings <!-- bla --> >
  <localRepository>$user.home/.m2/repository</localRepository>
</settings>

【讨论】:

我无法按照您的回答:/ 我知道您建议修改 .gradle 文件,然后运行应该生成等效 pom.xml 文件的 Gradle 命令?您能否指出我在具体示例中需要采取的步骤? 对不起。将上面的 gradle 代码添加到你的 gradle 文件中,然后运行 ​​gradle build install,这将构建 gradle 项目并将其安装到 mavenLocal。之后,将 localRepository 代码添加到您的 pom.xml &lt;settings/&gt; 块中,理论上您将能够使用 mavenLocal 存储库中的 gradle-build 工件。如果要安装到远程存储库,则需要更改远程存储库的 url 和凭据,然后运行 ​​gradle uploadArchives

以上是关于Maven:如何使用 build.gradle 文件?的主要内容,如果未能解决你的问题,请参考以下文章

Gradle build.gradle 到 Maven pom.xml

Netbeans 使用 pom.xml (Maven) 而不是 build.gradle (Gradle)

如何在build.gradle之外声明存储库详细信息

Gradle修改Maven仓库地址

如何将 Maven 构建转换为 Gradle?

将 maven 存储库添加到 build.gradle