简体   繁体   English

提供的常春藤依赖性

[英]Ivy dependency as provided

Problem : I need to have a lib on the eclipse classpath that should not be deployed to Tomcat. 问题 :我需要在eclipse类路径上有一个不应该部署到Tomcat的lib。 (In a maven project it would be scope provided) (在maven项目中,它将提供范围)

Explanation: 说明:

I've setup a project with some Ivy dependencies and had to externalize a configuration as JNI (mail/session) in order to do it I had to put the mail-1.4.7.jar inside the Tomcat lib folder. 我已经设置了一个带有一些Ivy依赖项的项目,并且必须将配置外部化为JNI(邮件/会话)才能完成它我必须将mail-1.4.7.jar放在Tomcat lib文件夹中。

The problem is that I have a dependency that add to my classpath the javax.mail-1.5.2.jar so I change it to: 问题是我有一个依赖项,将我的类路径添加到javax.mail-1.5.2.jar所以我将其更改为:

<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2">
    <exclude org="com.sun.mail" name="javax.mail"/>
</dependency>

The problem now is that my project break (compilation errors) because of missing mail classes such as javax.mail.MessagingException 现在的问题是我的项目因为缺少javax.mail.MessagingException类的邮件类而中断(编译错误)

So I have to add the mail dependency but only to eclipse. 所以我必须添加邮件依赖,但仅限于eclipse。 I've tried some configurations as explained here from what I know from Maven behavior with no avail. 我已经尝试了一些配置,如解释在这里从我从没有用Maven的行为知道。

Keeping the mail dependency only in the project, breaks Tomcat, keeping it on both tomcat and project breaks project. 仅保留邮件依赖项目,打破Tomcat,将其保留在tomcat和项目中断项目上。 When I manually remove it from my project lib folder (WEB-INF\\lib), after deploy the project, it works properly. 当我从项目lib文件夹(WEB-INF \\ lib)中手动删除它时,在部署项目后,它可以正常工作。

Bottom line (after deploy): 底线(部署后):

tomcatFolder                    
  |_lib
  |   |_...
  |   |_mail-1.4.7.jar
  |   |_...
  |_webapps
        |_myproject
             |_WEB-INF
                  |_lib
                     |_...
                     |_javax.mail-1.5.2.jar //need to remove it at deploy time only
                     |_...

Can't change it to maven right now. 现在无法将其更改为maven。 But it is in process :) 但它正在进行中:)

This is really a duplicate of this question: 这实际上是这个问题的重复:

But.. from your question I suspect you're not using ivy configuration mappings. 但是..从你的问题我怀疑你没有使用常春藤配置映射。 This is unfortunate because this is the mechanism used by ivy to logically group dependencies into functional groupings, similar to how Maven maintains scopes. 这是不幸的,因为这是常春藤用于逻辑地将依赖关系分组到功能分组中的机制,类似于Maven维护范围的方式。 The following posting attempts to bridge this understanding 以下帖子试图弥合这种理解

Furthermore you are also using Eclipse, which means that unless you're using the ivy plugin you effectively have two build mechanisms. 此外,您还使用Eclipse,这意味着除非您使用ivy插件 ,否则您实际上有两种构建机制。 (ivy and eclipse). (常春藤和日食)。 I would recommend fixing your ANT build first and then look at how to maintain the Eclipse classpath second. 我建议先修复你的ANT构建,然后再看看如何维护Eclipse类路径。

Example

The first section describes how configurations are declared and used in the ivy file and the second section explains how the ivy ANT tasks are used in the build logic. 第一部分描述了如何在常春藤文件中声明和使用配置,第二部分解释了如何在构建逻辑中使用常春藤ANT任务。

ivy.xml 的ivy.xml

You should always declare ivy configurations and use these to control your classpaths. 您应该始终声明常春藤配置并使用它们来控制类路径。 In my builds I always have at least three: compile, runtime and test. 在我的构建中,我总是至少有三个:编译,运行时和测试。 Notice how the extends attribute is used to create relationships between the configs, because runtime should also include the compile dependencies. 请注意extends属性如何用于在配置之间创建关系,因为运行时还应包括编译依赖项。

Adding an additional one for the provided scope jars is easy. 为提供的范围罐添加额外的一个很容易。 Simple stand-alone configuration: 简单的独立配置:

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile"  description="Required to compile application"/>
        <conf name="runtime"  description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"     description="Required for test only" extends="runtime"/>
        <conf name="provided" description="Needed for compile, but will be present on the target platform."/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>

        <!-- provided dependencies -->
        <dependency org="org.apache.tomcat" name="servlet-api" rev="6.0.16" conf="provided->master"/>
    </dependencies>

</ivy-module>

It's the configuration mappings that make things special. 这是使配置变得特别的配置映射。 The simple explanation is that they fall into two basic types when pulling from a Maven repository: 简单的解释是,从Maven存储库中提取时,它们分为两种基本类型:

  • conf="local_configuration->default" CONF = “local_configuration->默认”
  • conf="local_configuration->master" CONF = “local_configuration->主”

The first means include the remote module and all its dependencies. 第一种方法包括远程模块及其所有依赖项。 The second means include the remote module and exclude it's dependencies. 第二种方法包括远程模块并排除它的依赖性。 This means you don't need the following exclude trickery: 这意味着您不需要以下排除技巧:

<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2">
    <exclude org="com.sun.mail" name="javax.mail"/>
</dependency>

You simply use the following, if all you want is the log4j-core jar: 如果你想要的只是log4j-core jar,你只需使用以下命令:

<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2" conf="provided->master"/>

Additional notes: 补充笔记:

  • In ivy mapping to the remote "default" configuration will pull down only the jars you need. 在常春藤映射到远程“默认”配置将只下拉您需要的罐子。 It will exclude optional dependencies and other stuff like javadocs. 它将排除可选的依赖项和其他东西,如javadocs。
  • Sometimes "excludes" are necessary when module authors get their dependencies wrong. 当模块作者的依赖关系错误时,有时需要“排除”。

build.xml build.xml文件

The resolve target will pull down dependencies, generate a report and create the compile and test classpaths. 解决方案目标将下拉依赖项,生成报告并创建编译和测试类路径。 Note the use of configurations to determine which jar groupings should be used: 请注意使用配置来确定应使用哪些jar分组:

<target name="resolve" description="Use ivy to resolve classpaths">
    <ivy:resolve/>

    <ivy:report todir='${build.dir}/ivy-reports' graph='false' xml='false'/>

    <ivy:cachepath pathid="compile.path" conf="compile,provided"/>
    <ivy:cachepath pathid="test.path"    conf="test,provided"/>
</target>

These classpath references are then used by the compile target as normal: 然后,编译目标正常使用这些类路径引用:

<target name="compile" depends="resolve,resources" description="Compile code">
    <mkdir dir="${build.dir}/classes"/>
    <javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>

<target name="compile-tests" depends="compile" description="Compile tests">
    <mkdir dir="${build.dir}/test-classes"/>
    <javac srcdir="${test.src.dir}" destdir="${build.dir}/test-classes" includeantruntime="false" debug="true">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${build.dir}/classes"/>
        </classpath>
    </javac>
</target>

And the test target: 而测试目标:

<target name="test" depends="compile-tests" description="Run unit tests">
    <mkdir dir="${build.dir}/test-reports"/>
    <junit printsummary="yes" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${build.dir}/classes"/>
            <pathelement path="${build.dir}/test-classes"/>
        </classpath>
        <formatter type="xml"/>
        <batchtest fork="yes" todir="${build.dir}/test-reports">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test*.java"/>
                <exclude name="**/AllTests.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

Lastly the ivy retrieve task is used to build the war file. 最后,常春藤检索任务用于构建war文件。 Only the "runtime" configuration jars are used: 仅使用“运行时”配置jar:

<target name="package" depends="test" description="Create the WAR file">
    <ivy:retrieve pattern="${build.dir}/lib/[artifact].[ext]" conf="runtime"/>

    <war destfile="${war.file}" webxml="${resources.dir}/web.xml">
        <fileset dir="${resources.dir}" excludes="web.xml"/>
        <lib dir="${build.dir}/lib"/>
    </war>
</target>

In conclusion the cachepath ivy task is used to create classpath references based on the ivy configurations and the retrieve task is used when assembling the war file. 总之, cachepath ivy任务用于基于常春藤配置创建类路径引用,并且在组装war文件时使用检索任务。

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

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