andPat
andPat

Reputation: 4513

Use and Configure Maven by Java program, using Maven Java API

I'm using Maven Java API to configure Maven in a custom Java project. In particular I need to configure some Maven settings, among which there are proxy settings. How can i do this? I googled a lot, but I found no examples on how to use Maven from Java. Can You give me an example or a guide, a snippet of code, whatever you want to clarify HOW TO USE (AND CONFIGURE) Maven by Java API, i.e from Java code?

I found this maven reference, but what do I specifically need?

Thanks in advance.

I've already seen this question, but unfortunately there is no mention on how to edit settings.xml from maven api, I suppose it is possible, but I'm not sure of it, so I asked a new question, wider than that one, how can I manage Maven from Java? settings, run, properties, whatever... is it possible?

For example, about settings management, I found this API maven-settings, it can be useful? It's "read-only" API? I guess it isn't, but I've found no way how to "write" modifications to file, there are no examples on how to use it.

Upvotes: 2

Views: 189

Answers (1)

johnstosh
johnstosh

Reputation: 335

Well, yes, you are a bit crazy. You can take a look at some plug-ins which modify pom.xml files. For example, the versions-set facility shown here:

http://www.mojohaus.org/versions-maven-plugin/set-mojo.html

The source code for that plug-in will show you how to modify pom.xml files, but you also want to modify the settings.xml file.

All of these files are XML. Basically, you want to obtain a DOM for the .xml file. So, you can use generic XML tools to (1) read the file, (2) modify the document model, (3) write the data back to disk.

Note well: Maven caches the .xml files. You have to stop the maven executable and restart it to force it to re-read the .xml files. It sounds like you'll probably be doing this as a matter of course. :-)

Upvotes: 1

Related Questions