我有一個具有多個子項目的根項目。最初,我們將項目作爲單獨的Maven項目保留下來,但我意識到Gradle更適合我使用。但對於其中一個子項目,我們寧願將其轉換爲Gradle項目,但將其保存爲Maven項目。如何使用Gradle構建Maven項目作爲對根項目的依賴?
所以我試圖保留一個Maven項目作爲Gradle項目的一個子項目,但由於Maven項目pom.xml
中列出的依賴項未包括在內,所以構建失敗。下面是我的實驗
文件夾/項目結構
root (gradle root project)
|- api (maven project)
|- project (gradle subproject, depends on the "api" project)
根/ settings.gradle
rootProject.name = 'root'
def subDirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (!file.isDirectory()) {
return false
}
if (file.name == 'buildSrc') {
return false
}
return new File(file, 'build.gradle').isFile()
}
});
subDirs.each { File dir ->
include dir.name
}
根/的build.gradle
import org.gradle.api.artifacts.*
apply plugin: 'base' // To add "clean" task to the root project.
subprojects {
apply from: rootProject.file('common.gradle')
}
task mergedJavadoc(type: Javadoc, description: 'Creates Javadoc from all the projects.') {
title = 'All modules'
destinationDir = new File(project.buildDir, 'merged-javadoc')
// Note: The closures below are executed lazily.
source {
subprojects*.sourceSets*.main*.allSource
}
classpath.from {
subprojects*.configurations*.compile*.copyRecursive({ !(it instanceof ProjectDependency); })*.resolve()
}
}
根/ common.gradle
//
// This file is to be applied to every subproject.
//
apply plugin: 'java'
apply plugin: 'maven'
String mavenGroupId = 'com.mycompany.myproject'
String mavenVersion = '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
mavenCentral();
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}
dependencies {
// Adding dependencies here will add the dependencies to each subproject.
testCompile group: 'junit', name: 'junit', version: '4.10'
}
String mavenArtifactId = name
group = mavenGroupId
version = mavenVersion
task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from the source files.') {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives jar
archives sourcesJar
}
configure(install.repositories.mavenInstaller) {
pom.project {
groupId = mavenGroupId
artifactId = mavenArtifactId
version = mavenVersion
}
}
task createFolders(description: 'Creates the source folders if they do not exist.') doLast {
sourceSets*.allSource*.srcDirs*.each { File srcDir ->
if (!srcDir.isDirectory()) {
println "Creating source folder: ${srcDir}"
srcDir.mkdirs()
}
}
}
根/ API/pom.xml的
<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany.myproject</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/de.dev-eth0.dummycreator/dummy-creator -->
<dependency>
<groupId>de.dev-eth0.dummycreator</groupId>
<artifactId>dummy-creator</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>
根/項目/的build.gradle
if (!hasProperty('mainClass')) {
ext.mainClass = 'com.mycompany.myproject.HelloWorld'
}
dependencies {
compile project(":api")
}
根/ API/src目錄/主/ JAVA/COM/myCompany的/ myproject的/ API /原料藥的.java
package com.mycompany.myproject.api;
import org.dummycreator.DummyCreator;
public class Api {
public static void sayHello() {
System.out.println("Hello from API!");
DummyCreator dc = new DummyCreator();
Integer integer = dc.create(Integer.class);
System.out.println("Integer: " + integer);
}
}
當建設 「項目」,我得到下面的輸出:
Executing: gradle build
Arguments: [-c, C:\Users\birger\Desktop\test\root\settings.gradle]
C:\Users\birger\Desktop\test\root\api\src\main\java\com\mycompany\myproject\api\Api.java:3: error: package org.dummycreator does not exist
import org.dummycreator.DummyCreator;
^
C:\Users\birger\Desktop\test\root\api\src\main\java\com\mycompany\myproject\api\Api.java:10: error: cannot find symbol
DummyCreator dc = new DummyCreator();
^
symbol: class DummyCreator
location: class Api
C:\Users\birger\Desktop\test\root\api\src\main\java\com\mycompany\myproject\api\Api.java:10: error: cannot find symbol
DummyCreator dc = new DummyCreator();
^
symbol: class DummyCreator
location: class Api
3 errors
:api:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':api:compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 0.106 secs
Build failure (see the Notifications window for stacktrace): gradle build
那麼,將Maven子項目「按常規構建」作爲Gradle根項目的子項目,而實際上不必將其轉換爲Gradle項目最簡單的方法是什麼?
這可能有幫助,但它看起來並不像它的主動維護:https://github.com/uklance/gradle-maven-share (採取從這裏:https://stackoverflow.com/questions/40352951/include-gradle-module-to-maven-project) –
我建議將maven項目遷移到Gradle ...這就是它...什麼是這裏的問題? – khmarbaise
爲什麼你想做這樣的事情:(? – LazerBanana