2

我正在关注这个Springsource.org 教程,但我使用的是 Tomcat7,并且每当我运行“ant list”或“ant reload”或“ant start”时都会遇到以下错误。

/home/patchouli/workspace/springapp/build.xml:110: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at sun.net.NetworkClient.doConnect(NetworkClient.java:178)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.http.HttpClient.New(HttpClient.java:338)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:935)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:876)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:801)
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:210)
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:150)
at org.apache.catalina.ant.StartTask.execute(StartTask.java:44)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

这是我的 build.xml 文件:

<property file="build.properties"/>

<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="springapp"/>

<path id="master-classpath">
    <fileset dir="${web.dir}/WEB-INF/lib">
        <include name="*.jar"/>
    </fileset>

    <fileset dir="${appserver.lib}">
        <include name="servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
</path>

<target name="usage">
    <echo message=""/>
    <echo message="${name} build file"/>
    <echo message="-----------------------------------"/>
    <echo message=""/>
    <echo message="Available targets are:"/>
    <echo message=""/>
    <echo message="build     --> Build the application"/>
    <echo message="deploy    --> Deploy application as directory"/>
    <echo message="deploywar --> Deploy application as a WAR file"/>
    <echo message="install   --> Install application in Tomcat"/>
    <echo message="reload    --> Reload application in Tomcat"/>
    <echo message="start     --> Start Tomcat application"/>
    <echo message="stop      --> Stop Tomcat application"/>
    <echo message="list      --> List Tomcat applications"/>
    <echo message=""/>
</target>

<target name="build" description="Compile main source tree java files">
    <mkdir dir="${build.dir}"/>
    <javac destdir="${build.dir}" source="1.6" target="1.6" debug="true"
           deprecation="false" optimize="false" failonerror="true">
        <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
    </javac>
</target>

<target name="deploy" depends="build" description="Deploy application">
    <copy todir="${deploy.path}/${name}" preservelastmodified="true">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </copy>
</target>

<target name="deploywar" depends="build" description="Deploy application as a WAR file">
    <war destfile="${name}.war"
         webxml="${web.dir}/WEB-INF/web.xml">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </war>
    <copy todir="${deploy.path}" preservelastmodified="true">
        <fileset dir=".">
            <include name="*.war"/>
        </fileset>
    </copy>
</target>

<path id="catalina-ant-classpath">
    <fileset dir="${appserver.lib}">
           <include name="catalina-ant.jar"/>
           <include name="tomcat-coyote.jar"/>
           <include name="tomcat-util.jar"/>
        </fileset>
    <fileset dir="${appserver.home}/bin">
               <include name="tomcat-juli.jar"/>
    </fileset>
</path>  

<taskdef name="list" 
         classname="org.apache.catalina.ant.ListTask" 
         classpathref="catalina-ant-classpath"/>

<taskdef name="reload" 
         classname="org.apache.catalina.ant.ReloadTask" 
         classpathref="catalina-ant-classpath"/>

<taskdef name="start" 
         classname="org.apache.catalina.ant.StartTask" 
         classpathref="catalina-ant-classpath"/>

<taskdef name="stop" 
         classname="org.apache.catalina.ant.StopTask" 
         classpathref="catalina-ant-classpath"/>

<target name="reload">
    <reload url="${tomcat.manager.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"/>
</target>

<target name = "start">
    <start url="${tomcat.manager.url}"
                       username="${tomcat.username}"
                       password="${tomcat.password}"
                       path="/${webapp.name}"/>
</target>    

<target name = "stop">
       <stop url="${tomcat.manager.url}"
                         username="${tomcat.username}"
                         password="${tomcat.password}"
                         path="/${webapp.name}"
                         failonerror="false"/>
</target>

<target name="list">
    <list url="${tomcat.manager.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"/>
</target>

<target name="install">
    <install url="${tomcat.manager.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"
             war="${name}"/>
</target>

还有我的 build.properties 文件:

# Ant properties for building the springapp

appserver.home=/usr/share/tomcat7
appserver.lib=${appserver.home}/lib

deploy.path=/var/lib/tomcat7/webapps

tomcat.manager.url=http://localhost:8080/manager/html
tomcat.username=admin
tomcat.password=admin

我很沮丧。目前,我可以手动启动 tomcat7 并转到 localhost:8080/springapp/index.jsp 没有任何问题。但我希望使用 Apache Ant 实现自动化。问题似乎是 Ant 无法连接到 Tomcat。有什么想法吗?

4

4 回答 4

2

您似乎遇到了我在 Tomcat 7 上遇到的类似问题,并在ant target 上发布了将战争部署到 tomcat7/webapps的问题。

您需要进行两项更改:

1) 更新 Tomcat 7 的 Tomcat 管理器 URL(在 build.properties 中)

# Ant properties for building the springapp

tomcat.manager.url=http://localhost:8080/manager/text

2) 为用户添加额外的管理员角色(在 TOMCAT_DIRECTORY/conf/tomcat-users.xml 中)

    <role rolename="manager-script"/>

    <user username="admin" password="admin" roles="tomcat,manager-gui,manager-script"/>

在 ant 尝试部署 WAR 之前,请确保您的 TOMCAT 已经在运行。

于 2013-08-16T17:52:34.597 回答
0

在 build.properties 中的任何这些行之后是否有空格或制表符?

tomcat.manager.url=http://localhost:8080/manager/html
tomcat.username=admin
tomcat.password=admin

他们会得到字面上的对待。

于 2013-06-03T21:24:25.710 回答
0

我有同样的问题。如果您在 Windows 上,则路径可能会导致此问题,因为本教程基于 linux 操作系统。您可以尝试使用反斜杠 (\) 而不是 (/)

appserver.home=\usr\share\tomcat7
appserver.lib=${appserver.home}\lib

deploy.path=\var\lib\tomcat7\webapps

为了解决这个问题,我还用tomcat安装文件夹的绝对路径替换了变量。

于 2013-07-02T07:21:34.640 回答
0

在安装的地方手动启动 tomcat

  • bat/sh 路径:{tomcat--ver-}/bin/startup.

  • 然后运行 ​​build.xml

于 2014-06-26T05:43:42.667 回答