tomcat:deploy: “Cannot invoke Tomcat manager: Connection refused”

北战南征 提交于 2019-12-02 16:14:56

When using the tomcat 7 plugin, do the following:

pom.xml

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>tomcatserver</server>
        <path>/mycontext</path>
        <username>admin</username>
        <password>admin</password>
    </configuration>
</plugin>

settings.xml

<server>
    <id>tomcatserver</id>
    <username>admin</username>
    <password>admin</password>
</server>

tomcat-users.xml

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

run

mvn tomcat7:deploy

I had the same problem. In my case I fix it with run run-war instead of deploy for Tomcat7 with configuration:

         <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://localhost:8080</url>
                <path>/</path>
            </configuration>
        </plugin>

It took a while to understand the maven plugin deployment with Tomcat 7 mechanism but it works as follows:

For use with the command:

mvn tomcat7: deploy

Tomcat server must be initialized first! You can choose start from IDE or at startup script located in the folder %TOMCAT_HOME%\bin

The output from running it should look like this:

mvn tomcat7: run-war

Tomcat not needed be initialized because the script above will automatically start the server. However, the context /manager will not be enabled for use in the browser.

The output from running it should look like this:

You can even run the command mvn clean package to perform a previous cleaning of the project.

Some additional configurations:

[%TOMCAT_HOME%\conf] tomcat-users.xml

<role rolename="tomcat"/>
<role rolename="admin-script"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="manager"/>
<role rolename="admin"/>

<user password="tomcat" roles="tomcat" username="tomcat"/>
<user password="admin" roles="manager-gui" username="admin"/>
<user password="pass@word1" roles="admin,admin-script,manager-gui,manager-script,manager-jmx,manager-status" username="mavenuser"/>

[%MAVEN_HOME%\conf] settings.xml

<server>
    <id>your_server</id>
    <username>mavenuser</username>
    <password>pass@word1</password>
</server>

[%project%] pom.xml

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>your_server</server>
        <path>/project_context</path>
        <username>mavenuser</username>
        <password>pass@word1</password>                 
    </configuration>
</plugin>

Hope it helps!

ApriOri

I had the same problem and it appeared to be Tomcat version vs Maven plugin version. I used tomcat 7 and tried to deploy with tomcat:deploy-only instead of tomcat7:deploy only. If this is your case try tomcat7:deploy-xxx.

I think one important point here, is that you should use tomcat7:deploy. Do not overlook the 7.

I was using auto-completion and always ended with tomcat:deploy and just blindly ignored the little 7.

ziyao_zou

I got the similar problem:

Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat manager
    at org.apache.tomcat.maven.plugin.tomcat7.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:141)
    at org.apache.tomcat.maven.plugin.tomcat7.AbstractWarCatalinaMojo.execute(AbstractWarCatalinaMojo.java:68)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:115)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:181)
    at org.apache.http.impl.conn.LoggingSessionOutputBuffer.write(LoggingSessionOutputBuffer.java:73)
    at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115)
    at org.apache.tomcat.maven.common.deployer.TomcatManager$RequestEntityImplementation.writeTo(TomcatManager.java:880)
    at org.apache.http.entity.HttpEntityWrapper.writeTo(HttpEntityWrapper.java:89)
    at org.apache.http.impl.client.EntityEnclosingRequestWrapper$EntityWrapper.writeTo(EntityEnclosingRequestWrapper.java:108)
    at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:117)
    at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:265)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.sendRequestEntity(ManagedClientConnectionImpl.java:203)
    at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:236)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:682)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.tomcat.maven.common.deployer.TomcatManager.invoke(TomcatManager.java:742)
    at org.apache.tomcat.maven.common.deployer.TomcatManager.deployImpl(TomcatManager.java:705)
    at org.apache.tomcat.maven.common.deployer.TomcatManager.deploy(TomcatManager.java:388)
    at org.apache.tomcat.maven.plugin.tomcat7.deploy.AbstractDeployWarMojo.deployWar(AbstractDeployWarMojo.java:85)
    at org.apache.tomcat.maven.plugin.tomcat7.deploy.AbstractDeployMojo.invokeManager(AbstractDeployMojo.java:82)
    at org.apache.tomcat.maven.plugin.tomcat7.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:132)
    ... 23 more

I check the log of tomcat( dir:log/localhost_access.xxx, then I found that when maven upload the .wawr file to tomcat, the http status code of response is 403 , which means access was fobidden.

Then I checked that the manager and host app has the same dir: META-INF. in this dir, I found the file context.xml, this file limit the access of manager and host app. the key text is below:

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

Change the attribute allow to let your ip access manager and host app, then the problem solved.

If the problem is because of Java Heap Space. Then you can change it using following steps, if it is installed as a service:

1- Run from cmd "Tomcat_DIR\tomcat6w.exe" //ES//ServiceName

2- It will open tomcat GUI. In JAVA menu, give memomry pool configuration.

That much memory will be reserved for Tomcat. It should solve your memory heap problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!