[英]Ivy dependency as provided
問題 :我需要在eclipse類路徑上有一個不應該部署到Tomcat的lib。 (在maven項目中,它將提供范圍)
說明:
我已經設置了一個帶有一些Ivy依賴項的項目,並且必須將配置外部化為JNI(郵件/會話)才能完成它我必須將mail-1.4.7.jar
放在Tomcat lib文件夾中。
問題是我有一個依賴項,將我的類路徑添加到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和項目中斷項目上。 當我從項目lib文件夾(WEB-INF \\ 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。 但它正在進行中:)
這實際上是這個問題的重復:
但是..從你的問題我懷疑你沒有使用常春藤配置映射。 這是不幸的,因為這是常春藤用於邏輯地將依賴關系分組到功能分組中的機制,類似於Maven維護范圍的方式。 以下帖子試圖彌合這種理解
此外,您還使用Eclipse,這意味着除非您使用ivy插件 ,否則您實際上有兩種構建機制。 (常春藤和日食)。 我建議先修復你的ANT構建,然后再看看如何維護Eclipse類路徑。
第一部分描述了如何在常春藤文件中聲明和使用配置,第二部分解釋了如何在構建邏輯中使用常春藤ANT任務。
您應該始終聲明常春藤配置並使用它們來控制類路徑。 在我的構建中,我總是至少有三個:編譯,運行時和測試。 請注意extends屬性如何用於在配置之間創建關系,因為運行時還應包括編譯依賴項。
為提供的范圍罐添加額外的一個很容易。 簡單的獨立配置:
<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-core jar,你只需使用以下命令:
<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.2" conf="provided->master"/>
補充筆記:
解決方案目標將下拉依賴項,生成報告並創建編譯和測試類路徑。 請注意使用配置來確定應使用哪些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文件。 僅使用“運行時”配置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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.