你如何添加一个"系统" Gradle的依赖?

时间:2018-01-19 11:21:38

标签: java gradle

我的pom.xml看起来像这样:

<dependencies>
    <dependency>
        <groupId>javafx-packager</groupId>
        <artifactId>javafx-packager</artifactId>
        <version>1.8.0_40</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
    </dependency>
</dependencies>

如何在Gradle中执行相同操作?

更新:我尝试过这样做:

dependencies {
    compile files("${System.getenv('JAVA_HOME')}/../lib/ant-javafx.jar")
}

正如Giuseppe Ricupero所建议的那样,但我仍然遇到编译错误:

>gradle build

> Task :compileJava FAILED
C:\Users\pupeno\Documents\Dashman\code\custom-file-extension-windows-bundler\src\main\java\com\oracle\tools\packager\windows\CustomLauncherFileExtensionAppBundler.java:17: error: cannot find symbol
import static com.oracle.tools.packager.StandardBundlerParam.*;
                                       ^
  symbol:   class StandardBundlerParam
  location: package com.oracle.tools.packager
C:\Users\pupeno\Documents\Dashman\code\custom-file-extension-windows-bundler\src\main\java\com\oracle\tools\packager\windows\CustomLauncherFileExtensionAppBundler.java:18: error: cannot find symbol
import static com.oracle.tools.packager.windows.WindowsBundlerParam.*;
                                               ^
  symbol:   class WindowsBundlerParam
  location: package com.oracle.tools.packager.windows
C:\Users\pupeno\Documents\Dashman\code\custom-file-extension-windows-bundler\src\main\java\com\oracle\tools\packager\windows\CustomLauncherFileExtensionAppBundler.java:23: error: cannot find symbol
public class CustomLauncherFileExtensionAppBundler extends AbstractImageBundler {

我试图转换为Gradle的完整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>de.dynamicfiles.projects.javafx.bundler</groupId>
    <artifactId>custom-file-extension-windows-bundler</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <developers>
        <developer>
            <name>Danny Althoff</name>
            <email>fibrefox@dynamicfiles.de</email>
            <url>https://www.dynamicfiles.de</url>
        </developer>
    </developers>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <version.java.source>1.8</version.java.source>
        <version.java.target>1.8</version.java.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${version.java.source}</source>
                    <target>${version.java.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                        <manifestEntries>
                            <display_version>${display_version}</display_version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javafx-packager</groupId>
            <artifactId>javafx-packager</artifactId>
            <version>1.8.0_40</version>
            <scope>system</scope>
            <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
        </dependency>
    </dependencies>
</project>

更新2 :我尝试了像Giuseppe Ricupero推荐的gradle init,并提出了build.gradle

apply plugin: 'java'
apply plugin: 'maven'

group = 'de.dynamicfiles.projects.javafx.bundler'
version = '1.0.1-SNAPSHOT'

description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    system group: 'javafx-packager', name: 'javafx-packager', version: '1.8.0_40'
}

但是这不起作用,它会引发这个错误:

> Could not find method system() for arguments [{group=javafx-packager, name=javafx-packager, version=1.8.0_40}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

1 个答案:

答案 0 :(得分:2)

<强>更新

gradle init任务会自动(尝试?)将pom.xml转换为等效的gradle文件。你可以尝试一下:

  

的build.gradle

apply plugin: 'java'
apply plugin: 'maven'

group = 'de.dynamicfiles.projects.javafx.bundler'
version = '1.0.1-SNAPSHOT'

description = ""

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    system group: 'javafx-packager', name: 'javafx-packager', version:'1.8.0_40'
}
  

settings.gradle

rootProject.name = 'custom-file-extension-windows-bundler'

如果您将其与原始答案混合:

apply plugin: 'java'
apply plugin: 'maven'

group = 'de.dynamicfiles.projects.javafx.bundler'
version = '1.0.1-SNAPSHOT'

description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    compile files("${System.getenv('JAVA_HOME')}/lib/ant-javafx.jar")
}

它有效。

原始回答

我认为您只需要添加一个文件dep:

dependencies {
    compile files("${System.getenv('JAVA_HOME')}/../lib/ant-javafx.jar")
}

您可以在docs中看到更多内容。