提供的常春藤依赖 [英] Ivy dependency as provided

查看:61
本文介绍了提供的常春藤依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我需要在Eclipse类路径上有一个不应部署到Tomcat的库. (在Maven项目中,将提供范围)

Problem: I need to have a lib on the eclipse classpath that should not be deployed to Tomcat. (In a maven project it would be scope provided)

说明:

我已经建立了一个具有某些Ivy依赖项的项目,并且必须将配置外部化为JNI(邮件/会话),然后才能将mail-1.4.7.jar放入Tomcat lib文件夹中.

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.

问题是我有一个依赖项,将javax.mail-1.5.2.jar添加到我的类路径中,所以我将其更改为:

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:

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

现在的问题是由于缺少javax.mail.MessagingException

The problem now is that my project break (compilation errors) because of missing mail classes such as javax.mail.MessagingException

所以我必须添加邮件依赖项,但只能添加到eclipse中.我尝试了一些配置,如此处所述我从Maven行为中了解到的信息无济于事.

So I have to add the mail dependency but only to eclipse. I've tried some configurations as explained here from what I know from Maven behavior with no avail.

仅在项目中保留邮件依赖项会破坏Tomcat,将其保留在tomcat和project上都会破坏项目.当我从项目的lib文件夹(WEB-INF \ lib)中手动删除它时,在部署项目后,它可以正常工作.

Keeping the mail dependency only in the project, breaks Tomcat, keeping it on both tomcat and project breaks project. When I manually remove it from my project lib folder (WEB-INF\lib), after deploy the project, it works properly.

底线(部署后):

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

现在无法将其更改为Maven.但这正在过程中:)

Can't change it to maven right now. But it is in process :)

推荐答案

这确实是该问题的重复项:

This is really a duplicate of this question:

但是..从您的问题中我怀疑您没有使用ivy配置映射.这是不幸的,因为这是常春藤用来将依赖项逻辑地分组为功能性分组的机制,类似于Maven维护范围的方式.下面的帖子试图建立这种理解

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. The following posting attempts to bridge this understanding

此外,您还使用Eclipse,这意味着除非您使用 ivy插件您实际上有两种构建机制. (常食和日食).我建议先修复您的ANT构建,然后再研究如何维护Eclipse类路径.

Furthermore you are also using Eclipse, which means that unless you're using the ivy plugin you effectively have two build mechanisms. (ivy and eclipse). I would recommend fixing your ANT build first and then look at how to maintain the Eclipse classpath second.

第一部分说明如何在ivy文件中声明和使用配置,第二部分说明如何在构建逻辑中使用ivy ANT任务.

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.

您应该始终声明常春藤配置,并使用这些配置来控制类路径.在我的构建中,我总是至少有三个:编译,运行时和测试.请注意, extends 属性是如何用于在配置之间创建关系的,因为运行时还应包括编译依赖项.

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.

为提供的范围罐添加额外的一个很容易.简单的独立配置:

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>

使事情变得特别的是配置映射.简单的解释是,从Maven存储库中提取它们时,它们分为两种基本类型:

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:

  • conf ="local_configuration->默认"
  • conf ="local_configuration-> master"

第一种方法包括远程模块及其所有依赖项.第二种方法包括远程模块,并排除它的依赖项.这意味着您不需要以下排除诡计:

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>

如果只需要log4j-core jar,则只需使用以下内容:

You simply use the following, if all you want is the log4j-core jar:

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

其他说明:

  • 在常春藤中,映射到远程默认"配置将仅下拉您需要的jar.它将排除可选的依赖项和其他类似Javadocs之类的东西.
  • 当模块作者误解其依赖项时,有时排除"是必要的.

resolve目标将下拉依赖项,生成报告,并创建编译和测试类路径.请注意使用配置来确定应使用的jar分组:

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:

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

测试目标:

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

最近,常春藤检索任务用于构建war文件.仅使用运行时"配置jar:

Lastly the ivy retrieve task is used to build the war file. Only the "runtime" configuration jars are used:

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

最后, cachepath 常春藤任务是用于基于常春藤配置和检索任务在组装war文件时使用.

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.

这篇关于提供的常春藤依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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