重新编译Jar依赖

发布于 2024-10-01 05:29:09 字数 277 浏览 0 评论 0原文

假设我有一个项目依赖于 JAR A 中的类,该项目随后依赖于 JAR B 中的类。要运行该项目,两个 jar 需要位于同一类路径上。我有所有三个部分的源代码 - 项目、JAR A 和 JAR B。

如果我更改 JAR B 中类中方法的内部而不更改 API,我是否需要针对它重新编译 JAR A,或者可以我只是将它放入项目的类路径中然后就可以了?

如果我考虑一下,我认为我不需要,但我只是想仔细检查一下。当我只是尝试向 JAR B 添加额外的日志记录(这对 JAR A 没有影响)时,一直复制文件是非常烦人的。

Let's say I have a project with a dependency on a class in JAR A, which subsequently has a dependency on a class in JAR B. To run the project, both jars need to be on the same class path. I have the source code for all three pieces - project, JAR A, and JAR B.

If I change the internals of the method in the class in JAR B without changing the API, do I need to recompile JAR A against it, or can I just drop it into the classpath of the project and go?

If I think about it, I don't think I would need to but I just want to double check. It's quite annoying copying the files around all the time when I'm just trying to add extra logging to JAR B which has no effect on JAR A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2024-10-08 05:29:10

我认为你是对的:你只需重新创建包含新类的 JAR B 并将其与 JAR A 和 C 一起放入类路径中。

I think you're correct: you'd simply re-create the JAR B that contained the new class and put it in the class path along with JARs A and C.

小…楫夜泊 2024-10-08 05:29:10

填充 Jar A 的代码只需要能够编译即可创建该 jar。

如果它依赖 Jar B 进行编译,那么 Jar B 的存在需要满足 Jar A 的代码对其进行的所有引用。

反之亦然。

一旦 Jar A 的代码编译完毕,您就可以创建它的 jar 并忘记它。

然后,您可以随意更改 Jar B,只要 Jar A 使用的 API 不发生更改即可。

EG:

在 Jar B 中定义一个函数:

public class JarBClass
{
    public static void doSomething()
    {
        throw new RuntimeException();
    }
}

这会编译并且您可以创建 Jar B。

在 Jar A 中您引用该函数:

public class JarAClass
{
    public static void useSomething()
    {
        JarBClass.doSomething();
    }
}

这会编译并且您可以创建 Jar A,但是运行它会抛出异常。

您可以更新 Jar B 代码:

public class JarBClass
{
    public static void doSomething()
    {
        System.out.println("all good");
    }
}

这将编译并且您可以重新创建 Jar B。Jar A 可以毫无异常地运行。

但是,如果您更新 Jar B 并更改 API:

public class JarBClass
{
    public static void doSomething(String what)
    {
        System.out.println(what + " is all good");
    }
}

您将需要修改并重新编译 Jar A。

the code which populates Jar A only needs to be able to compile in order to create the jar.

If it relies on Jar B to compile, then Jar B needs to exist to the extent that it satisfies all of the references made to it by the code for Jar A.

The opposite is also true.

Once the code for Jar A has compiled, you can create it's jar and forget about it.

You can then change Jar B as much as you like as long as the API Jar A uses doesn't change.

EG:

In Jar B you define a function:

public class JarBClass
{
    public static void doSomething()
    {
        throw new RuntimeException();
    }
}

This compiles and you can create Jar B.

In Jar A you reference the function:

public class JarAClass
{
    public static void useSomething()
    {
        JarBClass.doSomething();
    }
}

This compiles and you can create Jar A, however running it would throw an exception.

You can update your Jar B code:

public class JarBClass
{
    public static void doSomething()
    {
        System.out.println("all good");
    }
}

This compiles and you can re-create Jar B. Jar A can run without exception.

However if you update Jar B and change the API:

public class JarBClass
{
    public static void doSomething(String what)
    {
        System.out.println(what + " is all good");
    }
}

You will need to modify and recompile Jar A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文