简体   繁体   English

有条件地排除与Ivy的某些依赖关系

[英]Excluding certain dependencies with Ivy, conditionally

My dependencies.xml contains a lot of packages required for my web application to run 我的dependencies.xml包含许多Web应用程序运行所需的软件包

Following are a few notable fragments 以下是一些值得注意的片段

<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>

I have package JTA from javax.transaction that is required for the application to run under Tomcat and forbidden for the application to run under WebSphere. 我从javax.transaction那里获得了JTA软件包,该软件包对于应用程序在Tomcat下运行是必需的 ,而对于在WebSphere下运行的应用程序则被禁止

I need to know how to make two different WAR files depending on the target platform. 我需要知道如何根据目标平台制作两个不同的WAR文件。 I don't exactly know how to use configurations, if that is the way. 如果那样的话,我不完全知道如何使用配置。

Ant will do an ivy-retrieve for configuration runtime and build a WAR archive using the jars downloaded from Artifactory. Ant将对配置runtime进行ivy-retrieve ,并使用从Artifactory下载的jar创建一个WAR存档。

I could exclude thos jar(s) manually by doing a delete after Ivy has resolved the artifacts, but hey, we are cool developers and we like to do the things the cleaner way. 我可以在Ivy解决工件后通过删除操作来手动排除thos jar,但是,嘿,我们很酷的开发人员,我们希望以更简洁的方式进行操作。

How would you suggest me to do an ivy-retrieve that targets Tomcat including JTA an another that targets Websphere excluding it? 您如何建议我做一个针对Tomcat(包括JTA)的针对ivy-retrieve ,另一个针对针对Websphere(不包括它) ivy-retrieve

build.xml : Building war files build.xml:构建war文件

The following fragment builds two war files: 以下片段构建了两个war文件:

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

The magic is that the tomcat retrieve task includes two configurations: 神奇之处在于,tomcat 检索任务包括两种配置:

<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 : Publishing build.xml:发布

The following answer contains more details on publishing multiple module artifacts to a Maven repository 以下答案包含有关将多个模块工件发布到Maven存储库的更多详细信息

So we need a target to generate the POM file: 因此,我们需要一个目标来生成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>

And a second target that publish the built files: 第二个发布构建文件的目标:

<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>

Take careful notice of the optional "classifier" attribute and note how the ivy file is structured next 请仔细注意可选的“ classifier”属性,并注意接下来的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>

Few things going on here. 这里发生的事情很少。

1) Make configuration mappings work for you 1)使配置映射适合您

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

2) The "extends" attribute is a set operation, meaning a compile dependency is automatically included as a runtime configuration. 2)“ extends”属性是一个set操作,这意味着编译依赖关系会自动包含为运行时配置。

An example is the SLF4J libary. 一个示例是SLF4J库。 The slf4j-api jar is required when compile code, where as the slf4j-log4j12 jar contained bindings and dependencies on log4j, a runtime (and changeable) dependency. 编译代码时需要slf4j-api jar,因为slf4j-log4j12 jar包含对log4j的绑定和依赖关系,即运行时(和可变)依赖关系。

3) The "master" configuration in your module is special and by convention in Maven world corresponds to the files published by this module. 3)模块中的“主”配置是特殊的,按照惯例,在Maven世界中,该配置对应于此模块发布的文件。

4) The "classifier" attribute is an example of a extra attribute in ivy. 4)“分类器”属性是常春藤中一个额外属性的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM