Patrick
Patrick

Reputation: 637

How to add dependencies to java project automatically based on the import statements?

I'm not familiar with using java package. Now I'm given a set of java files that import plenty of stuff. But there's no pom.xml file provided.

First I tried to directly compile the files, but was told com.amazonaws is not found. So I think I may need to download all the dependencies. But there are simply too many import statements so it seems not very practical to download them one by one manually.

Then I found that I may use maven. But after I tried creating this pom.xml

<dependencies>
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.341</version>
  </dependency>
</dependencies>

and use mvn package. I got an error telling me building failed. I thought that might be because I didn't specify all the dependencies. Is there a way to automatically pull dependencies based on import statements in the java files?

Upvotes: 2

Views: 1908

Answers (2)

Yash
Yash

Reputation: 128

Using MVN is best approach to add dependencies to you project. You need to write some more stuff in your pom.xml . You can go through this example https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html to create a maven project step by step

Upvotes: 0

paulsm4
paulsm4

Reputation: 121799

Maven is the right way to go. And you've made a good start.

Although Maven is very good about pulling "implicit dependencies", it can't "guess" everything. It sounds like you're going to have to add just a bit more to your pom.xml

SUGGESTIONS:

Upvotes: 1

Related Questions