BabelFish
BabelFish

Reputation: 909

Eclipse Add Project1 as dependent and auto import its dependents

I am trying to get used to Eclipse/Java but am more familiar with MS VisualStudio. Lets say I have Java Library (Project1) which has some dependencies on jar files via Properties->Java Build Path->Libraries (eg: AWS SDK, gson, swagger, etc). Now if I have Project2 and set a project dependency for Project2 to Project1 via Properties->Java Build Path->Project, I would hope that Project1 dependents would also be included for Project2. I dont see that happening or I am missing a step. I have been googling but I don't see any tutorial/documentation discussing 2 levels of dependents. I see that the Project1 jar is being referenced but what about the dependents for Project1? I am receiving an error such as:

The type XXXX cannot be resolved. It is indirectly referenced from required .class files XXXX

Upvotes: 0

Views: 92

Answers (3)

javaguest
javaguest

Reputation: 399

I strongly suggest using Maven, which is a great and easy to use dependency manager. Probably your eclipse already comes shipped with it, all you have to do is:

Do this for both projects:

Right click both projects, go to Configure -> Convert to Maven Project.

Create a group id,artirfact id and specify the version for your projects.

It will generate a pom.xml file in the root of your project. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<build>
    <sourceDirectory>src</sourceDirectory>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>

</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>
</project>

You can add dependencies for your projects just by adding a dependency tag.

    <dependency>
        <groupId>yourGroup</groupId>
        <artifactId>yourProject</artifactId>
        <version>1.0.0</version>
    </dependency>

After that just right click your projects go to

Run -> Run Configurations -> Maven Clean

Run -> Run Configurations -> Maven Install

and it will automatically download and install your dependencies for you.

Upvotes: 2

Verhagen
Verhagen

Reputation: 4034

This depends a little, on your project.

In case it is just a Java project, then it is better to use a build tool like Ant with Ivy, Maven or Gradle. As these contain the dependencies and other configuration details. Eclipse Mars (v4.5.1) comes with build in support for all these build tools.

In case it is an Eclipse Plug-in which you are developing, then you can configure it in Eclipse. And then store the configuration files, with the source code in the code repository.

Upvotes: 0

Ga&#235;l J
Ga&#235;l J

Reputation: 15050

You might want to have a look at Maven or a tool like this (Gradle, Ivy...) to handle your dependencies.

Relying on Eclipse for defining your build process (and dependencies) is a bad idea for long term projects.

Upvotes: 0

Related Questions