Include Java project as library

后端 未结 9 1891
眼角桃花
眼角桃花 2021-02-10 13:48

I have three projects in my eclipse workspace:

EventKitchenCore
EventKitchenDesktop
EventKitchenAndroid

EventKitchenCore contains

相关标签:
9条回答
  • 2021-02-10 14:17

    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. Then you can try to import your project again.

    0 讨论(0)
  • 2021-02-10 14:18

    You can configure your projects as a Maven Artifact that depends of EventKitchenCore, so, Maven will handle this for you:

    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:

    <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:

    <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.

    0 讨论(0)
  • 2021-02-10 14:26

    I would say as some contributors suggest, to use maven or 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. 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. 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.

    0 讨论(0)
  • 2021-02-10 14:29

    If all you want is to reference the Core projects code, then in Eclipse you can just reference this project from the Android project. (The 'library project' feature is only to Android library projects, you just have a normal Java project here, right?)

    So in your Package Explorer you have all three (Core, Android and Desktop) projects. Right click EventKitchenAndroid and select Properties. Choose Java Build Path -> Project tab and click the Add... button. Choose EventKitchenCore. Go to the Order & Export tab and make sure the EventKitchenCore project is checked and you're good to go. Any change to the source of EventKitchenCore is immediatedly available to the Android project.

    0 讨论(0)
  • 2021-02-10 14:32

    This is the way i follow to add library to my projects

    Project with library

    PHASE 1: Creating Library Project

    Here on image you can see 2 projects named library and ListSamples which is using library

    To make the project library as a library project right click on the project and select properties

    Setting Library

    Mark IsLibrary check box to make it library

    you can see .jar file in bin folder of library project

    Compiled Libary

    Now the library project is created

    PHASE 2 : Adding Library Project to android Project

    Now Right click on your android project and select its property. then click on Add button inside library area and set the path of your library project to it

    enter image description here

    enter image description here

    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)

    enter image description here

    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

    enter image description here

    While checking your android project you can see the .jar file of your project is been added to your android project

    enter image description here

    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 .

    You can also use ant to add library to projects. But i don't have knowledge about that .

    0 讨论(0)
  • 2021-02-10 14:33

    Eclipse has builtin Ant support. You can make use of it to automatically create a JAR of the current project and put it in a folder of another project.

    Provided that EventKitchenAndroid and EventKitchenCore projects are both in the same workspace, create a build.xml file in EventKitchenCore project which contains just this:

    <?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. If it works the way you want, then you can tell Eclipse to automatically execute this build file everytime the project is built. Go to the project properties of EventKitchenCore and in the Builders property, click Import... and select the build.xml file you just created.

    Now, it will be executed everytime the project is built. You can manually force the build of a project by pressing Ctrl+B. See also the video demo I just created.

    0 讨论(0)
提交回复
热议问题