自定义Maven打包,不包含传递依赖项 [英] Custom Maven packaging, transitive dependencies aren't included

查看:167
本文介绍了自定义Maven打包,不包含传递依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义的Maven 3打包插件来处理一些非Java工件,并且在使传递依赖项起作用时遇到了问题.我定义了三个项目,分别是 model model-impl cli ,其依赖项如下:

I'm playing around with a custom Maven 3 packaging plugin for some non-java artifacts, and having an issue getting transitive dependencies to work. I've got three projects defined, model, model-impl, and cli, with dependencies like this:

cli
  model-impl
    model

在每个项目中都会调用我的自定义生命周期插件,并且我可以成功构建 model model-impl .对于每个项目,预期的工件都存储在我的本地资源库中.但是 cli 失败了,因为在我的Mojo中我没有得到 model 作为依赖.我不完全确定这是我的代码中的问题,因为即使使用 mvndependency:dependency-tree 也不显示完整的依赖关系层次结构:

My custom lifecycle plugins are being called in each project, and I can successfully build model and model-impl. For each of those projects, the expected artifacts are being stored in my local repository. cli however is failing because I don't get model as a dependency in my Mojo. I'm not completely sure that this is a problem in my code though, because even using mvn dependency:dependency-tree doesn't show the full dependency hierarchy:

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.corp.nodes:myproj-cli >-----------------------
[INFO] Building myproj Test 1.0.0-SNAPSHOT
[INFO] --------------------------------[ myproj ]--------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ myproj-cli ---
[INFO] com.corp.nodes:myproj-cli:myproj:1.0.0-SNAPSHOT
[INFO] \- com.corp.:myproj-model-impl:myproj:1.0.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

在这里,我期望每个项目看到的树都有三个级别.

Here I would have expected that I see a tree with three levels for each of the projects.

这是在我的自定义插件中定义的components.xml:

Here's the components.xml defined in my custom plugin:

<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
            <role-hint>myproj</role-hint>
            <implementation>
                org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
            </implementation>
            <configuration>
                <phases>
                    <initialize>com.corp.maven:myproj-plugin:unpackageDependencies</initialize>
                    <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
                    <compile>com.corp.maven:myproj-plugin:compile</compile>
                    <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources>
                    <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
                    <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
                    <package>com.corp.maven:myproj-plugin:package</package>
                    <install>org.apache.maven.plugins:maven-install-plugin:install</install>
                    <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
                </phases>
            </configuration>
        </component>
        <component>
            <role>org.apache.maven.artifact.handler.ArtifactHandler</role>
            <role-hint>myproj</role-hint>
            <implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
            <configuration>
                <type>myproj</type>
                <extension>myproj</extension>
                <packaging>myproj</packaging>
                <addedToClasspath>true</addedToClasspath>
                <includesDependencies>true</includesDependencies>
            </configuration>
        </component>
    </components>
</component-set>

所有项目POM的包装都设置为 myproj ,所有依赖项的类型都设置为 myproj .这是 cli 项目的pom:

All of the project POMs have their packaging set to myproj, and all of the dependencies have their type set to myproj. Here's the pom for the cli project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.corp.arthur.nodes</groupId>
    <artifactId>myproj-cli</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>myproj</packaging>

    <name>myproj Test</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.corp.arthur</groupId>
            <artifactId>myproj-model-impl</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>myproj</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.corp.maven</groupId>
                <artifactId>myproj-plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

impl 项目的POM看起来很相似.知道我想念的是什么吗?

The POM for the impl project looks similar. Any idea's what I'm missing?

推荐答案

最后通过跟踪DefaultDependencyCollector类的源找出了发生了什么.原来,我需要在我的components.xml中将includesDependencies设置为 false .发生的事情是,依赖关系收集器看到该标志为true,这意味着依赖关系已包含在工件中,因此没有通过它们进行递归.将其设置为false时,它确实会递归,并且我得到了预期的行为.

Finally figured out what was going on by tracing through the source for the DefaultDependencyCollector class. Turns out I needed to set includesDependencies in my components.xml to false. What was happening is the dependency collector saw that flag was true, meaning the dependencies were included in the artifact, and so didn't recurse through them. With it set to false, it does recurse, and I get the expected behavior.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.corp.nodes:myproj-cli >------------------
[INFO] Building myproj Test: CLI Node 1.0.0-SNAPSHOT
[INFO] --------------------------------[ myproj ]--------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ myproj-cli ---
[INFO] com.corp.nodes:myproj-cli:myproj:1.0.0-SNAPSHOT
[INFO] \- com.corp:myproj-model-impl:myproj:1.0.0-SNAPSHOT:compile
[INFO]    \- com.corp:myproj-model:myproj:1.0.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

这篇关于自定义Maven打包,不包含传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆