0

I am trying to integrate maven with a old style java project to manage dependencies.

To simply download the jars, I could use

mvn dependency:get -DrepoUrl=https://mvnrepository.com -Dartifact=artifactId:groupId:version

But due to some product challenges, I need to add these jars to classPath myself. So if there is some kind of tool/solution to help get these details automatically. Like what all jars got download as part of a specific pom.xml

Example: I could run the above command inside java code with

Runtime.exec()

And parse the output myself, but I was wondering if there is a better way instead of me parsing all that.

Also, please suggest me any better way to deal with such a situation.

PS: I cannot convert the project to Maven project using any tools etc(since I would change the structure of project) rather I have get the similar behavior myself

Update

I found that I could use maven-embedder.jar but I couldn't find much help. I want to use only for downloading the dependency i.e my command to do that would be like below:

mvn test dependency:build-classpath -f "C:\Users\Desktop\My_Pom.xml" -DoutputDirectory="C:\ProgramData\MyDirectoeyRepository"

How could I do this?

user578219
  • 597
  • 2
  • 9
  • 32
  • How do you build the project currently? What is the current layout of the project directory? – Thorbjørn Ravn Andersen May 24 '18 at 20:23
  • Current Proj layout has no "src" directory: ProjName/pckg/myJave.java The application is an Eclipse RCP app, so it takes care of building.But I am planning to use maven to resolve dependencies – user578219 May 25 '18 at 04:47
  • If you cannot reorganize your sources so they are somehow isolated you are in for a bit of pain with maven. Perhaps look into Ivy to create an ant build? – Thorbjørn Ravn Andersen May 25 '18 at 06:53

2 Answers2

1

I am not sure I understand everything correctly, but using dependency:copy-dependencies (https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html) you can download all dependencies of a given pom to a given directory.

This allows you to put them on the classpath of some java project.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks. But since its my plain old java project, I need to put it into all the jars to BuildPath for compile time. And due to some project implementation details. i have to put these to my own ClassLoader for it work fine at runtime. So to do that, I need a list of files that got downloaded, so that I can add to my ClassLoader. Since its not a maven project, the downloaded files wont be put into "Maven Dependencies" buildpath container and also I am doing all these using a commandline. So tool like maven.jar that can help me with all these. – user578219 May 25 '18 at 07:03
  • Does https://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html help you? – J Fabian Meier May 25 '18 at 07:32
  • Thanks, This will be helpful. I will try. – user578219 May 26 '18 at 07:32
  • Thanks, that was helpful. Is there any utility by Apache or 3rd party for using these options from with java code? – user578219 May 26 '18 at 16:21
  • I mean any API that could help me instead of using mvn command using rt.exec(Command) – user578219 May 29 '18 at 04:51
1

I ended up using Maven-Embedder's MavenCli class.

user578219
  • 597
  • 2
  • 9
  • 32