How can I embed Maven 2 into my application?

2019-05-07 06:19发布

I'd like to embed Maven or the library that does all the magic into my Java application.

The keypoints :

  • They are two tasks I want to perform :

    1/ Publishing a jar in local repository
    2/ Publishing a jar in a private enterprise repository (Nexus)

  • All required jars MUST be located in a public Maven repository

  • The jars SHOULD be Maven version agnostic (ie not specific to Maven 2 or 3)

If you can , please provide a snippet with your response.

3条回答
一夜七次
2楼-- · 2019-05-07 06:42

I have not tried what you want, but I would start by looking into the Maven Embedder project. Possibly even the m2e project which also comes with an embedded version of Maven (and the ability to use external installations too).

Publishing locally will likely involve invoking the maven-install-plugin, and publishing remotely will likely involve using the maven-deploy-plugin.

Hope this points you in the right direction.

查看更多
三岁会撩人
3楼-- · 2019-05-07 06:57

Maven clients push content to Nexus using a normal HTTP "POST" operation. If all you want to do is publish content, then you don't need all the logic for downloading and resolving dependencies....

If you decide you need full-blown Maven repository interoperability then I'd suggest emulating what other projects like Groovy, Gradle and Scala have done, which is to embed Apache Ivy.

I found the following article describing how to add ivy into your java project (Single jar dependency):

http://developers-blog.org/blog/default/2010/11/08/Embed-Ivy-How-to-use-Ivy-with-Java

Groovy Example

Your question is specifically how to add support for publishing content.

The following code uses ivy to publish to a Nexus repo. Groovy enables you to use Ivy's documented ANT tasks.

import groovy.xml.NamespaceBuilder
import groovy.xml.MarkupBuilder

// Methods
// =======
def generateIvyFile(String fileName) {
    def file = new File(fileName)

    file.withWriter { writer ->
        xml = new MarkupBuilder(writer)

        xml."ivy-module"(version:"2.0") {
            info(organisation:"org.dummy", module:"dummy")
            publications() {
                artifact(name:"dummy", type:"pom")
                artifact(name:"dummy", type:"jar")
            }
        }
    }

    return file
}

def generateSettingsFile(String fileName) {
    def file = new File(fileName)

    file.withWriter { writer ->
        xml = new MarkupBuilder(writer)

        xml.ivysettings() {
            settings(defaultResolver:"central")
            credentials(host:"myrepo.com" ,realm:"Sonatype Nexus Repository Manager", username:"deployment", passwd:"deployment123")
            resolvers() {
                ibiblio(name:"central", m2compatible:true)
                ibiblio(name:"myrepo", root:"http://myrepo.com/nexus", m2compatible:true)
            }
        }
    }

    return file
}

// Main program
// ============
def ant = new AntBuilder()
def ivy = NamespaceBuilder.newInstance(ant, 'antlib:org.apache.ivy.ant')

generateSettingsFile("ivysettings.xml").deleteOnExit()
generateIvyFile("ivy.xml").deleteOnExit()

ivy.resolve()
ivy.publish(resolver:"myrepo", pubrevision:"1.0", publishivy:false) {
    artifacts(pattern:"build/poms/[artifact].[ext]")
    artifacts(pattern:"build/jars/[artifact].[ext]")
}
查看更多
smile是对你的礼貌
4楼-- · 2019-05-07 07:00

Here is an example embedding Maven 2 libraries:

Instead of looking up the project builder, you can look up the ArtifactInstaller and ArtifactDeployer - you'll find the code you want in the maven-install-plugin and the maven-deploy-plugin.

The difference just POSTing in this example is that it will generate the appropriate metadata, checksums and snapshot transformations.

The Maven 3 libraries may be easier to embed and remain compatible with Maven 2, however I don't have any examples readily available.

查看更多
登录 后发表回答