gradle中的本地Jar依赖项:如何导入代码

时间:2019-11-02 13:21:15

标签: java gradle import jar dependencies

我正在构建一个具有testone.jar依赖项的gradle项目

testone.jar是一个工作的.jar,带有一个Brew类,用于打印内容。

我想打电话给testone.Brew来自Main.java。

" import testone.Brew; " gives me a compile error

“>渐变构建”有效,”>渐变依赖项”显示:

implementation - Implementation only dependencies for source set 'main'. (n)
\--- unspecified (n)

文件夹结构:

GradleTest/

----build/

--------libs/

------------GradleTest.jar

----libs/

--------testone.jar

----src/

--------test/

--------main/net/alexhublon/gradletest/

--------------------------------------Main.java

build.gradle:

apply plugin: 'java'

repositories {
    flatDir {
        dirs 'libs';
    }
}

dependencies {
    implementation files("libs/testone.jar")
}

sourceSets {
    main.java.srcDir 'src/main'
    test.java.srcDir 'src/test'
}

jar {
    manifest.attributes 'Main-Class': 'net.alexhublon.gradletest.Main'
}

这是Main.java的代码:

package net.alexhublon.gradletest;

import java.util.Arrays;
//import libs/testone.jar???
public class Main
{
    public static void main (String[] args)
    {
        System.out.println("Finished");
                //testone.Brew brew = new Brew()???
    }
}

我想从testone.jar导入和使用功能

编辑: 如果使用该类编译.jar文件,则该文件有效。我相信问题出在Gradle

1 个答案:

答案 0 :(得分:0)

就像使用Arrays一样简单地导入它:

package net.alexhublon.gradletest;

import java.util.Arrays;
import testone.Brew;

public class Main
{
    public static void main (String[] args)
    {
        System.out.println("Finished");
        Brew brew = new Brew()
    }
}

https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html