Problem:我需要一个不应该部署到Tomcat的eclipse类路径库。(在maven项目中将提供范围)
解释:
我已经设置了一个带有一些常春藤依赖项的项目,并且必须将一个配置外部化为JNI (mail/session),为了做到这一点,我必须将mail-1.4.7.jar
放在Tomcat文件夹中。
问题是,我有一个依赖项添加到我的类路径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>
现在的问题是,由于缺少邮件类(如javax.mail.MessagingException
),我的项目中断(编译错误)。
因此,我必须添加邮件依赖项,但只能添加到eclipse。我尝试了一些配置,如我从Maven行为中了解到的那样解释了这里,但没有效果。
只在项目中保留邮件依赖,破坏Tomcat,将其保留在tomcat和项目中断项目上。当我从项目库文件夹(WEB\ lib )手动删除它时,在部署项目之后,它将正常工作。
底线(部署后):
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。但它正在进行中:)
发布于 2016-06-18 00:37:01
这其实是一个重复的问题:
但是..。从您的问题中,我怀疑您没有使用常春藤配置映射。这是不幸的,因为这是常春藤用于将依赖关系逻辑分组为功能分组的机制,类似于Maven维护作用域的方式。下面的帖子试图将这种理解联系起来
此外,您还在使用Eclipse,这意味着除非您使用的是常春藤插件,否则您实际上有两种构建机制。(常春藤和月食)我建议先修复ANT构建,然后再看看如何维护Eclipse类路径。
示例
第一节描述如何在常春藤文件中声明和使用配置,第二节解释如何在构建逻辑中使用常春藤任务。
ivy.xml
您应该始终声明常春藤配置,并使用这些配置来控制类路径。在我的构建中,我总是至少有三个:编译、运行时和测试。请注意扩展属性是如何用于在信任项之间创建关系的,因为运行时还应该包含编译依赖项。
为提供的作用域jars添加一个额外的jars很容易。简单的独立配置:
<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存储库中提取时,它们分为两种基本类型:
第一种方法包括远程模块及其所有依赖项。第二种方法包括远程模块,并排除它的依赖关系。这意味着您不需要以下排除欺骗:
<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2">
<exclude org="com.sun.mail" name="javax.mail"/>
</dependency>
您只需使用以下内容,如果您只需要log4j核心jar:
<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2" conf="provided->master"/>
补充说明:
build.xml
“解决”目标将提取依赖项,生成报告,并创建编译和测试类路径。注意使用配置来确定应该使用哪些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>
然后,这些类路径引用被编译目标正常地使用:
<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文件。只使用“运行时”配置jars:
<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>
https://stackoverflow.com/questions/37840659
复制相似问题