简体   繁体   English

在Eclipse上使用Ant创建战争之前,刷新不起作用

[英]Refresh doesn't work before creating a war with Ant on Eclipse

On Eclipse I create war files by using ant. 在Eclipse上,我使用ant创建war文件。

The issue is that in the war file isn't included the right mypropfile.properties . 问题在于war文件中没有包含正确的mypropfile.properties The file is properly copied, but also if I use <eclipse.refreshLocal resource="projectdir" depth="infinite"/> the old file is included. 该文件已正确复制,但是如果我使用<eclipse.refreshLocal resource="projectdir" depth="infinite"/> ,则也会包含旧文件。 I have to refresh manually the project. 我必须手动刷新项目。

For Ant I use the "Run in the same JRE as the workspace" option. 对于Ant,我使用“在与工作空间相同的JRE中运行”选项。

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" basedir=".">
<description>
    My Project
</description>

<property name="workspace.dir" value="${basedir}/../../"/>
<property name="src" value="${basedir}/../src"/>
<property name="build" value="${basedir}/../build"/>
<property name="build.classes" value="${basedir}/../build/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="web.dir" value="${basedir}/WEB-INF"/>
<property environment="env"/>
<property name="real.dir" value="${basedir}/real"/>
<property name="real2.dir" value="${basedir}/real2"/>

<path id="classpath.server">  
    <fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"/>  
    <pathelement path="${build.classes}"/>
</path>

<path id="classpath.app">  
    <fileset dir="${lib.dir}" includes="*.jar"/>  
</path> 

<target name="refreshResource" if="eclipse.refreshLocal">
    <eclipse.refreshLocal resource="projectdir" depth="infinite"/>
</target>

<target name="clean">
    <delete dir="${build}/classes"/>
    <delete dir="${build}"/>
</target>

<target name="init" depends="clean, refreshResource">
    <tstamp/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build}/classes"/>
</target>

<target name="compile" depends="init">
    <javac encoding="UTF8" srcdir="${src}" destdir="${build}/classes"    includeantruntime="false">
        <compilerarg value="-Xlint:unchecked"/>
        <classpath>  
            <path refid="classpath.server.bin"/>  
        </classpath>
        <classpath>  
            <path refid="classpath.server"/>  
        </classpath> 
        <classpath>
            <path refid="classpath.app"/>
            <fileset dir="${lib.dir}" includes="*.jar"/>  
         </classpath>
    </javac>
</target>

<target name="deleteConfig">
    <delete file="${src}/mypropfile.properties"/>
</target>

<target name="real" depends="deleteConfig">
    <copy file="${real.dir}/realprop.properties" tofile="${src}/mypropfile.properties"/>
</target>

<target name="real2" depends="deleteConfig">
    <copy file="${real2.dir}/real2prop.properties" tofile="${src}/mypropfile.properties"/>
</target>

<target name="war-real" depends="real, compile">
    <input message="Warname (without .war):" addproperty="warname"/>
    <war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
        <fileset dir="${basedir}">
            <include name="**/*.*"/>
        </fileset>
        <classes dir="${build.classes}"/>
    </war>
</target>

<target name="war-real2" depends="real2, compile">
    <input message="Warname (without .war):" addproperty="warname"/>
    <war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
        <fileset dir="${basedir}">
          <include name="**/*.*"/>
        </fileset>
        <classes dir="${build.classes}"/>
    </war>
</target>

EDIT 编辑

The target clean was wrong, so I've corrected it, but now build failed with error 目标清理错误,因此我已纠正它,但现在构建因错误而失败

BUILD FAILED ... Reference classpath.server.bin not found.

Ant doesn't care if Eclipse has refreshed the file or not. Ant不在乎Eclipse是否刷新了文件。 eclipse.refreshLocal is only relevant for editors and compilers inside of the IDE. eclipse.refreshLocal仅与IDE内的编辑器和编译器相关。

When you run the Ant build.xml , Ant copies the file in question in the real target into the source folder and compile copies it into ${build}/classes (at least it should do that). 当您运行Ant build.xml ,Ant将real目标中的相关文件复制到源文件夹中,然后compile复制到${build}/classes (至少应该这样做)。 So before you create the WAR, you must make sure the compile step has done its work (ie look into each file to make sure that a change is visible in each copy). 因此,在创建WAR之前,必须确保compile步骤已完成工作(即查看每个文件以确保每个副本中都可见更改)。

What worries my is that you use different ways to access the classes : 我担心的是您使用不同的方式访问这些classes

  • ${build}/classes
  • ${build.classes}
  • ${basedir}/../build/classes

So the first step should be to define a single way to locate the folder and then use this pattern everywhere. 因此,第一步应该是定义一种找到文件夹的单一方法,然后在所有地方使用此模式。

If that doesn't solve your problem, you need to make sure Ant notices that the file has changed. 如果那不能解决您的问题,则需要确保Ant注意到文件已更改。 Old filesystems like FAT support only timestamps which have second resolution. FAT等旧文件系统仅支持具有第二个分辨率的时间戳。 If you use an USB stick for your sources, it's possible to change the file and run Ant so fast that Ant thinks the file hasn't changed. 如果您使用USB记忆棒作为源,则可以更改文件并以如此之快的速度运行Ant,以至于Ant认为文件没有更改。

Lastly, you need to check your classpath. 最后,您需要检查您的类路径。 If one of the JAR dependencies also contains a file called mypropfile.properties , then Java resource loading can find either version. 如果一个JAR依赖项还包含一个名为mypropfile.properties的文件,则Java资源加载可以找到任何一个版本。

This and other problems made me use a different solution to configure WAR files: I pass a system property with the absolute path of the config file. 这个问题和其他问题使我使用了不同的解决方案来配置WAR文件:我将系统属性与配置文件的绝对路径一起传递。 That way, the WAR file doesn't change when the config changes and I have full control over which config file is loaded. 这样,当配置更改时,WAR文件不会更改,并且我可以完全控制要加载的配置文件。

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

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