How to convert Gradle to Maven

1. Gradle Build File

示例引用 Spring 实战(第 4 版)第 1 章 Spring 之旅
https://github.com/habuma/spring-in-action-4-samples

/Users/wuyong/spring

git clone https://github.com/habuma/spring-in-action-4-samples.git

/Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

jar {
    baseName = 'knight'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile("org.springframework:spring-context:${springVersion}")
    compile("org.aspectj:aspectjweaver:${aspectJVersion}")
    compile("log4j:log4j:${log4jVersion}")

    testCompile("junit:junit:${junitVersion}")
    testCompile("org.mockito:mockito-core:${mockitoVersion}")
    testCompile("org.springframework:spring-test:${springVersion}")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

2. Maven Plugin

apply plugin: 'maven'
group = 'org.example'
version = '0.0.1-SNAPSHOT'

/Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight

修改 build.gradle 文件内容如下:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

jar {
    baseName = 'knight'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    mavenLocal()
    mavenCentral()
}

group = 'org.example'
version = '0.0.1-SNAPSHOT'

dependencies {
    compile("org.springframework:spring-context:${springVersion}")
    compile("org.aspectj:aspectjweaver:${aspectJVersion}")
    compile("log4j:log4j:${log4jVersion}")

    testCompile("junit:junit:${junitVersion}")
    testCompile("org.mockito:mockito-core:${mockitoVersion}")
    testCompile("org.springframework:spring-test:${springVersion}")
}

3. Convert

/Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight

gradle install

/Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight/build/poms

pom-default.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>knight</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.7.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.0.7.RELEASE</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

4. Import Maven Project to Eclipse

  <packaging>jar</packaging>

  <name>knight</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

/Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight

拷贝 pom-default.xml 到当前目录,修改文件名为 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>org.example</groupId>
  <artifactId>knight</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>knight</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.7.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.0.7.RELEASE</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

保留 src 目录和 pom.xml 文件,其他文件全部删除。

Eclipse 导入 /Users/wuyong/spring/spring-in-action-4-samples/Chapter_01/knight 目录。

参考

https://codexplo.wordpress.com/2014/07/20/gradle-to-maven-conversion-and-vice-versa/
https://dzone.com/articles/how-to-convert-maven-to-gradle-and-vice-versa
https://www.baeldung.com/gradle-build-to-maven-pom

posted @ 2020-10-02 19:26  clipboard  阅读(1964)  评论(0编辑  收藏  举报