如何在两个项目之间共享代码?

编程入门 行业动态 更新时间:2024-10-24 06:28:32
本文介绍了如何在两个项目之间共享代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个名为A和B的Java项目。它们都是部署为war文件的Web应用程序。我分别在Eclipse工作区中创建它们。项目B使用项目A中名为 MusicMapper 的类。我按照这篇文章中的建议,将项目A添加到项目B的Eclipse构建路径中/ A>。所以现在项目B可以编译而没有任何错误,并且可以在项目B中通过在项目B中导入它来看到类 MusicMapper :

I have two Java projects called A and B. Both of them are web apps deployed as war files. I created them in Eclipse workspace separately. Project B uses a class in project A named MusicMapper. I added the project A to project B's build path in Eclipse as suggested in this post. So now project B can compile without any errors and the class MusicMapper can be seen in project B by importing it in project B:

import com.projectA.MusicMapper;

在推出项目B的网络应用程序之前,一切似乎都没问题。但是,当我启动项目时项目B并在项目A中调用引用类 MusicMapper 的代码,我得到以下运行时错误:

Everything seems to be fine before I launched the web app of project B. However, when I launched the project B and called the code that references class MusicMapper in project A, I got the following runtime error:

java.lang.ClassNotFoundException: com.projectA.MusicMapper

所以这个错误似乎是由从项目A导入的未完成的类 com.projectA.MusicMapper 引起的。因为我已经将项目A添加到项目B构建路径并且项目B编译良好而没有任何错误,为什么它会在运行时报告此错误?

So this error seems to be caused by unfound class com.projectA.MusicMapper which is imported from project A. Since I already added project A to project B build path and project B compiles fine without any errors, why does it report this error at runtime?

我采取的另一种方法是:我还尝试在项目B的 pom.xml中使用以下Maven导入 :

Another approach I took was: I've also tried using the following Maven import in project B's pom.xml:

<dependency> <groupId>com.projectA</groupId> <artifactId>projectA</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>system</scope> <systemPath>/path/to/projectA.jar</systemPath> </dependency>

其中 projectA.jar 是jar文件我从项目A导出但它仍然给了我相同的 ClassNotFoundException 。如何使项目A中的类可以被项目B使用?似乎我试过的这两种方法都不起作用。

where projectA.jar is the jar file I exported from project A. But it still gave me the same ClassNotFoundException. How can I make the class in project A usable by project B? It seems that neither of the two approaches I've tried works.

推荐答案

首先,如果你有两个项目都被部署为战争,那么将一个项目纳入另一个项目并不是一个好主意。作为依赖。因为你将在项目B中不需要很多其他东西。

First of all if you have two projects which are both deployed as wars it's not a good idea to include one project into another as a dependency. Because you will be puling in lot of other stuff that you don't need in project B.

更好的方法是创建一个第三个java项目让说Common这应该只是一个 java项目和 NOT 一个动态Web项目,应该编译为jar。将项目A和项目B之间共享的所有内容移动到Common项目中。

A better approach will be to create a Third java project lets say "Common" this should be just a java project and NOT a Dynamic Web Project and should compile as a jar. Move all the stuff that is shared between Project A and Project B into Common project.

现在支持您的问题只需在项目构建中添加一些内容eclipse中的路径并不意味着你已经在eclipse之外添加了对项目的依赖。 Eclipse不会抱怨,因为它可以解析并找到项目,因为你在eclipse构建路径中添加了它。您必须使用某些构建工具(如ant,maven或gradle)编译公共依赖项目,并在构建war时将jar打包在war文件中。

Now back your problem Just adding some thing in project build path in eclipse does not mean you have added the dependency to your project outside of eclipse as well. Eclipse don't complain because it can resolve and find the project because you added in eclipse build path. You will have to compile common dependency project using some build tool like ant, maven or gradle and package the jar in your war file when war is built.

如果你不想使用构建工具,一个简单的路径就是将第二个项目从eclipse导出为jar。在从Eclipse中导出war文件之前,复制项目A和项目B的WEB-INF / lib文件夹中的公共jar文件。

If you don't want to use a build tool a simple route would be just export the third project from eclipse as jar. Copy the common jar file in WEB-INF/lib folder of your Project A and Project B before you export the war file from your eclipse.

我希望它有帮助:)

更多推荐

如何在两个项目之间共享代码?

本文发布于:2023-11-25 22:37:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   代码   项目   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!