如何将 Maven 嵌入到我的应用程序中?

发布于 2024-12-18 01:46:09 字数 280 浏览 3 评论 0 原文

我想将 Maven 或能够发挥所有作用的库嵌入到我的 Java 应用程序中。

要点:

  • 这是我想要执行的两项任务:

    1/ 在本地存储库中发布 jar
    2/ 在私有企业存储库(Nexus)中发布 jar

  • 所有必需的 jar 必须位于公共 Maven 存储库中

  • jar 应该是 Maven版本无关(即不特定于 Maven 2 或 3)

如果可以的话,请提供您的回复的片段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-12-25 01:46:09

Maven 客户端使用正常的 HTTP“POST”操作将内容推送到 Nexus。如果您只想发布内容,那么您不需要下载和解决依赖关系的所有逻辑...

如果您决定需要成熟的 Maven 存储库互操作性,那么我建议模仿其他项目,例如 Groovy ,Gradle和Scala都做了,就是嵌入Apache Ivy

我发现以下文章描述了如何将ivy添加到您的java项目中(单jar依赖项):

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

Groovy 示例

您的问题是具体来说如何添加对发布内容的支持。

以下代码使用 ivy 发布到 Nexus 存储库。 Groovy 使您能够使用Ivy 记录的 ANT 任务

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]")
}

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]")
}
栖竹 2024-12-25 01:46:09

以下是嵌入 Maven 2 库的示例:

而不是查找项目构建器,您可以查找 ArtifactInstallerArtifactDeployer - 您将在 maven-install-pluginmaven-deploy-plugin

本示例中 POST 的区别在于它将生成适当的元数据、校验和和快照转换。

Maven 3 库可能更容易嵌入并与 Maven 2 保持兼容,但是我没有任何可用的示例。

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.

ぺ禁宫浮华殁 2024-12-25 01:46:09

我还没有尝试过你想要的,但我会首先研究 Maven Embedder 项目。甚至可能是 m2e 项目,它也附带了 Maven 的嵌入式版本(并且也能够使用外部安装)。

本地发布可能涉及调用 maven-install-plugin,远程发布可能涉及使用 maven-deploy-plugin。

希望这能为您指明正确的方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文