【问题标题】:Excluding certain dependencies with Ivy, conditionally有条件地排除与 Ivy 的某些依赖项
【发布时间】:2017-06-04 11:49:54
【问题描述】:

我的 dependencies.xml 包含许多我的 Web 应用程序运行所需的包

以下是一些值得注意的片段

<configurations>
    <conf name="test" visibility="public" extends="compile" />
    <conf name="compile" visibility="public" extends="runtime" />
    <conf name="runtime" visibility="public" />
    <conf name="provided" visibility="public" />
    <conf name="junit" visibility="public" extends="test" />
</configurations>


<publications>
    <artifact name="${project.name}" type="jar" ext="jar" conf="compile" />
    <artifact name="${project.name}" type="zip" ext="zip" conf="compile"/>
</publications>


<dependencies>


    <dependency org="org.hibernate"                     name="hibernate-core"                   rev="5.1.3.Final"       transitive="false"          conf="runtime->*"/>
    <dependency org="org.hibernate"                     name="hibernate-ehcache"                rev="5.1.3.Final"       transitive="false"          conf="runtime->*"/>
    <dependency org="org.hibernate.common"              name="hibernate-commons-annotations"    rev="5.0.1.Final"       transitive="false"          conf="runtime->*"/>
    <dependency org="org.hibernate.javax.persistence"   name="hibernate-jpa-2.1-api"            rev="1.0.0.Final"       transitive="false"          conf="runtime->*"/>
    <dependency org="org.javassist"                     name="javassist"                        rev="3.21.0-GA"         transitive="false"          conf="runtime->*"/>
    <dependency org="org.jboss.logging"                 name="jboss-logging"                    rev="3.3.0.Final"       transitive="false"          conf="runtime->*"/>
    <dependency org="javax.transaction"                 name="jta"                              rev="1.1"               transitive="false"          conf="runtime->*"/>
    <dependency org="net.sf.ehcache"                    name="ehcache-core"                     rev="2.6.11"            transitive="false"          conf="runtime->*"/>
    <dependency org="antlr"                             name="antlr"                            rev="2.7.7"             transitive="false"          conf="runtime->*"/>
    <dependency org="org.antlr"                         name="antlr4-runtime"                   rev="4.5.2-1"           transitive="false"          conf="runtime->*"/>

</dependencies>

我有来自 javax.transaction 的 JTA 包,对于在 Tomcat 下运行的应用程序 必需 和在 WebSphere 下运行的应用程序 禁止。

我需要知道如何根据目标平台制作两个不同的 WAR 文件。我不完全知道如何使用配置,如果是这样的话。

Ant 将为配置 runtime 执行 ivy-retrieve 并使用从 Artifactory 下载的 jar 构建 WAR 存档。

我可以在 Ivy 解决工件后通过删除手动排除这些 jar,但是嘿,我们是很酷的开发人员,我们喜欢以更清洁的方式做事。

你会如何建议我做一个ivy-retrieve 以包含 JTA 的 Tomcat 为目标,另一个以 Websphere 为目标但不包括它?

【问题讨论】:

    标签: java ant dependency-management ivy


    【解决方案1】:

    build.xml : 构建战争文件

    以下片段构建了两个war文件:

    • ../demo.war
    • ../demo-websphere.war

    神奇的是tomcat的retrieve任务包含两种配置:

    <ivy:retrieve pattern="${lib.dir}/tomcat/[artifact].[ext]" conf="runtime,tomcat_only"/>
    
    <war destfile="${dist.dir}/demo.war" webxml="${resources.dir}/web.xml">
      <fileset dir="${resources.dir}" excludes="web.xml"/>
      <lib dir="${lib.dir}/tomcat"/>
    </war>
    
    <ivy:retrieve pattern="${lib.dir}/websphere/[artifact].[ext]" conf="runtime"/>
    
    <war destfile="${dist.dir}/demo-websphere.war" webxml="${resources.dir}/web.xml">
      <fileset dir="${resources.dir}" excludes="web.xml"/>
      <lib dir="${lib.dir}/websphere"/>
    </war>
    

    build.xml : 发布

    以下答案包含有关将多个模块工件发布到 Maven 存储库的更多详细信息

    所以我们需要一个目标来生成 POM 文件:

    <target name="prepare" description="Generate POM">
        <!-- Optional: Intermediate file containing resolved version numbers -->
        <ivy:deliver deliverpattern="${build.dir}/ivy.xml" pubrevision="${publish.revision}" status="release"/>
    
        <!-- Generate the Maven POM -->
        <ivy:makepom ivyfile="${build.dir}/ivy.xml" pomfile="${build.dir}/demo.pom"/>
    </target>
    

    以及发布构建文件的第二个目标:

    <target name="publish" depends="init,prepare" description="Upload to Nexus">
        <ivy:publish resolver="nexus-deploy" pubrevision="${publish.revision}" overwrite="true" publishivy="false" >
            <artifacts pattern="${build.dir}/[artifact](-[classifier]).[ext]"/>
        </ivy:publish>
    </target>
    

    注意可选的“分类器”属性并注意 ivy 文件接下来的结构

    ivy.xml

    <ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
      <info organisation="com.myspotontheweb" module="demo"/>
    
      <configurations>
        <conf name="master"/>
        <conf name="default" extends="master,runtime"/>
        <conf name="compile"/>
        <conf name="provided"/>
        <conf name="runtime" extends="compile"/>
        <conf name="test"    extends="runtime"/>
        <conf name="tomcat_only" description="A special configuration for special tomcat only dependencies"/>
      </configurations>
    
      <publications>
        <artifact name="demo" type="war" conf="master"/>
        <artifact name="demo" type="pom" conf="master"/>
        <artifact name="demo" type="war" conf="master" e:classifier="websphere"/>
      </publications>
    
      <dependencies>
        <!-- Compile dependencies -->
        <dependency org="org.hibernate" name="hibernate-core" rev="5.1.3.Final" conf="compile->default"/>
        <dependency org="org.api" name="slf4j-api" rev="1.7.22" conf="compile->default"/>
    
        <!-- Runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.22" conf="runtime->default"/>
    
        <!-- Tomcat dependencies -->
        <dependency org="javax.transaction" name="jta" rev="1.1" conf="tomcat_only->master"/>
      </dependencies>
    
    </ivy-module>
    

    这里发生的事情很少。

    1) 让配置映射为您服务

    config1->default   # Remote artifact plus transitive dependencies
    config2->master    # Remote artifact only (Same as setting transitive=false)
    

    2) “extends”属性是一个设置操作,意味着编译依赖会自动包含在运行时配置中。

    一个例子是SLF4J 库。编译代码时需要 slf4j-api jar,因为 slf4j-log4j12 jar 包含对 log4j 的绑定和依赖,这是一个运行时(和可更改)依赖。

    3) 您模块中的“主”配置是特殊的,按照 Maven 世界的约定,对应于该模块发布的文件。

    4) “分类器”属性是常春藤中extra attribute 的示例。

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2014-06-06
      • 2016-11-20
      • 1970-01-01
      • 2018-04-13
      • 2015-03-06
      • 2015-12-28
      • 2013-03-11
      • 1970-01-01
      相关资源
      最近更新 更多