简体   繁体   English

将Java项目包含为库

[英]Include Java project as library

I have three projects in my eclipse workspace: 我的eclipse工作区中有三个项目:

EventKitchenCore
EventKitchenDesktop
EventKitchenAndroid

EventKitchenCore contains all the core functionality, and EventKitchenDesktop and EventKitchenAndroid are essentially just different user interfaces. EventKitchenCore包含所有核心功能,而EventKitchenDesktopEventKitchenAndroid本质上只是不同的用户界面。

I have EventKitchenCore added and working as a library in EventKitchenDesktop , however I am unable to figure out how to add it as a library to EventKitchenAndroid . 我有EventKitchenCore添加和工作在图书馆EventKitchenDesktop ,但我无法弄清楚如何将其添加为一个库EventKitchenAndroid

How do I do this? 我该怎么做呢? It would be a pain in the tush to have to export as a jar in the lib directory every time I make a change... 每次我做出改变时,必须在lib目录中导出为jar是一件痛苦的事情......

I tried adding the Core project in Properties > Java Build Path > Libraries and Properties > Android > Library , but no luck :\\ 我尝试在Properties > Java Build Path > Libraries and Properties > Android > Library添加Core项目,但没有运气:\\

Update - for now, I got it working by exporting the core project as a jar in the libs folder. 更新 - 现在,我通过将核心项目导出为libs文件夹中的jar来实现它。 However I make frequent changes, and like I said, it's a pain in the tush. 然而,我经常变化,就像我说的那样,这是一种痛苦。

Eclipse has builtin Ant support. Eclipse内置了Ant支持。 You can make use of it to automatically create a JAR of the current project and put it in a folder of another project. 您可以使用它来自动创建当前项目的JAR并将其放在另一个项目的文件夹中。

Provided that EventKitchenAndroid and EventKitchenCore projects are both in the same workspace, create a build.xml file in EventKitchenCore project which contains just this: 如果EventKitchenAndroidEventKitchenCore项目都在同一个工作区中,请在EventKitchenCore项目中创建一个build.xml文件,其中包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<project name="EventKitchenCore" default="createjar">
    <target name="createjar">
        <jar destfile="../EventKitchenAndroid/libs/EventKitchenCore.jar" basedir="bin" />
    </target>
</project>

To test it, select the file in Eclipse and press Ctrl + F11 to run it as Ant build file. 要测试它,请在Eclipse中选择该文件,然后按Ctrl + F11将其作为Ant构建文件运行。 If it works the way you want, then you can tell Eclipse to automatically execute this build file everytime the project is built. 如果它按照您想要的方式工作,那么您可以告诉Eclipse在每次构建项目时自动执行此构建文件。 Go to the project properties of EventKitchenCore and in the Builders property, click Import... and select the build.xml file you just created. 转到EventKitchenCore的项目属性,在Builders属性中,单击Import ...并选择刚刚创建的build.xml文件。

Now, it will be executed everytime the project is built. 现在,它将在每次构建项目时执行。 You can manually force the build of a project by pressing Ctrl + B . 您可以通过按Ctrl + B手动强制构建项目。 See also the video demo I just created. 另请参阅我刚创建的视频演示

You can configure your projects as a Maven Artifact that depends of EventKitchenCore, so, Maven will handle this for you: 您可以将项目配置为依赖于EventKitchenCore的Maven工件 ,因此,Maven将为您处理:

EventKitchenCore pom.xml: EventKitchenCore pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-core</artifactId>
   <packaging>jar</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenCore</name>
</project>

EventKitchenDesktop pom.xml: EventKitchenDesktop pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-desktop</artifactId>
   <packaging>jar</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenDesktop</name>
   <dependencies>
     <dependency>
       <groupId>com.adamgaskins</groupId>
       <artifactId>event-kitchen-core</artifactId>
       <version>0.0.1-SNAPSHOT</version>
     </dependency>
   <dependencies>
</project>

EventKitchenAndroid pom.xml: EventKitchenAndroid pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-android</artifactId>
   <packaging>apk</packaging> <!-- for this, you need the maven-android-plugin -->
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenAndroid</name>
   <dependencies>
     <dependency>
       <groupId>com.adamgaskins</groupId>
       <artifactId>event-kitchen-core</artifactId>
       <version>0.0.1-SNAPSHOT</version>
     </dependency>
   <dependencies>
</project>

Don't forget of SNAPSHOT suffix, otherwise, Maven will not update your changes. 不要忘记SNAPSHOT后缀,否则,Maven将不会更新您的更改。

The simplest idea (which would work outside of Eclipse, too) would be to create a symlink/shortcut from your package-directory of the library into the src -folder of your projects: 最简单的想法(也可以在Eclipse之外工作)是从库的package-directory到项目的src -folder创建一个符号链接/快捷方式:

|- EventKitchenCore
   |- src/
      |- org.your.library.package <-+
|- EventKitchenDesktop              |
   |- src/                          |
      |- org/desktop/something/     |
      |- symlink to ----------------+
|- EventKitchenAndroid              |
   |- src/                          |
      |- org/android/something/     |
      |- symlink to ----------------+

This way, you could compile the actual projects as if your library would be a part of the applications and you have your changes immediately reflected in both your projects. 这样,您可以编译实际项目,就像您的库将成为应用程序的一部分一样,并且您的更改会立即反映在您的项目中。

I would say as some contributors suggest, to use maven or ant. 我会说,正如一些贡献者所建议的那样,使用maven或ant。 The configuration is easy, most of them have already put them, so it is a matter of copy and paste. 配置很简单,大多数已经放置它们,所以这是复制和粘贴的问题。 When you are in the development environment, what you can do is to add the projects in your build path. 当您在开发环境中时,您可以做的是在构建路径中添加项目。 Go to build path and select the "projects" tab and add the projects you need, so every time you make changes, all the changes are visible for the projects that are linked. 转到构建路径并选择“项目”选项卡并添加所需的项目,因此每次进行更改时,所有更改都对链接的项目可见。 When you are about to deploy, you can then use the maven or ant. 当您即将部署时,您可以使用maven或ant。 However, you can start with both of them in development as well. 但是,您也可以从开发过程开始。 But if you want a quick fix, I would suggest to reference the projects if you don't want to use maven or ant yet. 但是如果你想快速修复,我建议你参考这些项目,如果你不想使用maven或ant。 But when you deploy I strongly recommend to use one of this two project managers, because as you mentioned, export the projects as jars and then add them to the libraries folder is a pain. 但是当你部署时我强烈建议使用这两个项目管理器中的一个,因为正如你所提到的,将项目导出为jar然后将它们添加到libraries文件夹是一件痛苦的事。

Here is a solution that may help you : 这是一个可以帮助您的解决方案:

1) Eclipse-Package Explorer - Your Project(Android) - Rightclick on your project. 1)Eclipse-Package Explorer - 您的项目(Android) - 右键单击​​您的项目。

2) Click on Android (At left side of screen) - Now your screen may look like this - 2)点击Android(屏幕左侧) - 现在您的屏幕可能如下所示 -

在此输入图像描述

Now at bottom of screen, in Library section you can add all the library project directly without making any JAR. 现在在屏幕底部,在“库”部分中,您可以直接添加所有库项目,而无需进行任何JAR。 Just you need to import those library projects in workspace or all your library project should be opened in eclipse. 只需要在工作区中导入这些库项目,或者所有库项目都应该在eclipse中打开。

1) Click on Add button you will get the list of library projects available, choose from them and Build your project. 1)单击“添加”按钮,您将获得可用的库项目列表,从中进行选择并构建项目。

Good Luck.. 祝好运..

You have to go to the preferences of the project you want to make a library and check Is Library in the Android sub-menu. 您必须转到要创建库的项目首选项,然后在Android子菜单中选中Is Library Then you can try to import your project again. 然后,您可以尝试再次导入项目。

I believe what you are looking for is the "Link Source" option within Eclipse. 我相信您正在寻找的是Eclipse中的“链接源”选项。 From your android project, select the project and right-click to select "Build Path" > "Link Source". 从您的Android项目中,选择项目并右键单击以选择“构建路径”>“链接源”。 This will allow you to have your Java library accessible within the same build path as your android app for development. 这将允许您在与Android应用程序开发相同的构建路径中访问您的Java库。

If all you want is to reference the Core projects code, then in Eclipse you can just reference this project from the Android project. 如果你想要的只是引用Core项目代码,那么在Eclipse中你可以从Android项目中引用这个项目。 (The 'library project' feature is only to Android library projects, you just have a normal Java project here, right?) (“图书馆项目”功能仅适用于Android库项目,您在这里只有一个普通的Java项目,对吧?)

So in your Package Explorer you have all three (Core, Android and Desktop) projects. 因此,在您的Package Explorer中,您拥有所有三个(Core,Android和Desktop)项目。 Right click EventKitchenAndroid and select Properties. 右键单击EventKitchenAndroid并选择Properties。 Choose Java Build Path -> Project tab and click the Add... button. 选择Java Build Path - > Project选项卡,然后单击Add ...按钮。 Choose EventKitchenCore . 选择EventKitchenCore Go to the Order & Export tab and make sure the EventKitchenCore project is checked and you're good to go. 转到Order&Export选项卡,确保选中EventKitchenCore项目并且您很高兴。 Any change to the source of EventKitchenCore is immediatedly available to the Android project. 任何对EventKitchenCore源的更改都可以立即用于Android项目。

This is the way i follow to add library to my projects 这是我将库添加到项目中的方法

项目与图书馆

PHASE 1: Creating Library Project 第1阶段:创建图书馆计划

Here on image you can see 2 projects named library and ListSamples which is using library 在图像上,您可以看到2个名为libraryListSamples的项目正在使用库

To make the project library as a library project right click on the project and select properties 要将项目作为库项目,请右键单击项目并选择属性

设置库

Mark IsLibrary check box to make it library 选中Mark IsLibrary复选框以使其成为库

you can see .jar file in bin folder of library project 你可以在库项目的bin文件夹中看到.jar文件

编译的Libary

Now the library project is created 现在创建了库项目

PHASE 2 : Adding Library Project to android Project 第2阶段:将库项目添加到android项目

Now Right click on your android project and select its property. 现在右键单击你的android项目并选择它的属性。 then click on Add button inside library area and set the path of your library project to it 然后单击库区域内的“ 添加”按钮,并将库项目的路径设置为它

在此输入图像描述

在此输入图像描述

Then a pop- will appear showing a list of library projects you have created in eclipse which is presently active (closed library projects will not shown) 然后会出现一个弹出窗口,显示您在eclipse中创建的目前处于活动状态的库项目列表(不会显示已关闭的库项目)

在此输入图像描述

Here i got only one project so it is listed here 在这里,我只有一个项目,所以它在这里列出

Select which all library you needed and click OK . 选择所需的所有库,然后单击“ 确定” Now the library projects will be added to your project 现在,库项目将添加到您的项目中

在此输入图像描述

While checking your android project you can see the .jar file of your project is been added to your android project 在检查你的android项目时,你可以看到项目的.jar文件已被添加到你的android项目中

在此输入图像描述

NOTE :- If you are using android support package( android-support-v4.jar ) in library project , There is no need to add it again in your Android Project . 注意: -如果您在库项目中使用Android支持包( android-support-v4.jar ),则无需在Android项目中再次添加它。

You can also use ant to add library to projects. 您还可以使用ant将库添加到项目中。 But i don't have knowledge about that . 但我对此并不了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 java gradle 项目中包含对库的引用 - How to include reference to library in java gradle project 使用Gradle将我自己的Java库包含在项目中 - Using Gradle to include my own Java library in a project 如何在Android Studio中的现有Android项目中包含Java库 - How to include java library in existing Android project in Android Studio 如何在我的Java项目中完全包含Batik库? - How do I fully include the Batik library in my Java project? 在Android库项目aar构件中包括来自Java库子模块的源 - Include sources from Java library submodule in Android library project aar artifact 如何在Java项目中包含.jar库,并在编译后使应用程序使用该库在应用程序内部? - How to include .jar library, that is inside java project and make application use this library that is inside application after compilation? 将Java库包含到自己的库中 - Include java libraries into own library 如何在Eclipse项目(Java)中永久包含外部库? 这样,当将其移植到另一台计算机时,它仍然存在 - How to permanently include an external library in an Eclipse project (Java)? So that when porting it to another computer, it's still there Android项目中的Java库 - Java Library in Android Project 如何在Android Studio中的库项目中包含库项目 - How to include library project in library project in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM