tomcat:deploy: "Cannot invoke Tomcat manager: Connection refused"
Asked Answered
M

9

40

I tried to deploy my app to Tomcat using Maven and I've found this sample.

That's fragment of my pom.xml:

    <!-- Maven Tomcat Plugin -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
            <url>http://127.0.0.1:8080/</url>
            <server>TomcatServer</server>
        </configuration>
    </plugin>
    <!-- Maven compiler plugin -->
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>

And when I run tomcat:deploy I get this error: Cannot invoke Tomcat manager: Connection refused. That's full stacktrace:

[ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project webapp: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project webapp: Cannot invoke Tomcat manager
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat manager
    at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:149)
    at org.codehaus.mojo.tomcat.AbstractWarCatalinaMojo.execute(AbstractWarCatalinaMojo.java:70)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 25 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:227)
    at sun.net.www.http.HttpClient.New(HttpClient.java:300)
    at sun.net.www.http.HttpClient.New(HttpClient.java:317)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at org.codehaus.mojo.tomcat.TomcatManager.invoke(TomcatManager.java:597)
    at org.codehaus.mojo.tomcat.TomcatManager.deployImpl(TomcatManager.java:662)
    at org.codehaus.mojo.tomcat.TomcatManager.deploy(TomcatManager.java:295)
    at org.codehaus.mojo.tomcat.AbstractDeployWarMojo.deployWar(AbstractDeployWarMojo.java:85)
    at org.codehaus.mojo.tomcat.AbstractDeployMojo.invokeManager(AbstractDeployMojo.java:85)
    at org.codehaus.mojo.tomcat.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:141)
    ... 28 more

I didn't want to use authentication so I just skipped those steps. I thought that this may cause my error, but when I tried to add them, nothing changed. Do you know where's my mistake?

UPD: Sorry, but I can't check our solutions because I'm not working on this project anymore. But I will not close this question so if you think that your solution will work feel free to left it here and may be it will help to someone.

Maciemaciel answered 14/3, 2013 at 13:38 Comment(4)
Is Tomcat running and bound to localhost? What does netstat -an tell you?Kurtz
@Anders Seems like it is not running. I tried to launch Tomcat using startup.bat and then launch tomcat:deploy from IDE aaaand... java.lang.OutOfMemoryError: Java heap space. I tried to debug and have found that it causes when maven try to copy some 128MB size array.Maciemaciel
I had the stupid issue of not running the starutp.bat of Tomcat. Be sure to check that.Watanabe
Try running mvn tomcat:run instead of mvn tomcat:deployKobe
W
30

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
Wickliffe answered 5/3, 2014 at 2:43 Comment(6)
Just in case is useful to someone, the settings.xml must be on your .m2 folder. You may need to create it.Lisk
By doing it this way you don't need the pwd in your pom file. This is how the whole file looks:<settings xmlns="maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/SETTINGS/1.0.0 maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>localhost</id> <username>script-tomcat</username> <password>tomcat</password> </server> </servers> </settings>Lisk
this works like charm...have to provide username password at both placesEulaeulachon
Where do you put the settings.xml and tomcat-users.xml?Araucania
@botenvouwer, should be under the conf directory in the tomcat installation folder.Wickliffe
Downvote please fix. Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat managerVolost
P
18

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>
Putman answered 25/1, 2015 at 11:38 Comment(1)
thanks.. worked for me but run-war uses an embedded tomcat implying I lose my datasource configurationFredrick
G
11

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: enter image description here

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: enter image description here

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!

Guitarist answered 17/11, 2015 at 18:45 Comment(3)
run-war uses an embedded tomcat, so it's a completely different setup (maybe that's fine for some cases though.) "Runs the current project as a packaged web application using an embedded Tomcat server."Peseta
to deploy Tomcat server must be initialized first! tnxEnt
you should start tomcat first then run your build mvnRubenstein
S
6

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.

Scoop answered 10/10, 2013 at 13:28 Comment(0)
C
2

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.

Corot answered 28/4, 2016 at 16:6 Comment(1)
Not sure why you got tagged twice, but you are exactly right.Carlsbad
I
1

So when I was using mvn tomcat7:deploy I was facing this issue though everything was rightly configured .

The issue was that before deploying I had to undeploy i.e mvn tomcat7:undeploy and then tomcat7:deploy.

Ooops ! that's such a crazy error.

Inflow answered 22/4, 2021 at 16:21 Comment(1)
this definitely worked for me with tomcat 10.1.5 and jdk 11 with tomcat running as a service. thanks so muchDoth
D
0

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.

Doctrine answered 1/11, 2017 at 11:17 Comment(0)
S
0

I use XAMPP and i only managed when i edited My server setting to edit the correct tomcat-user.xml...

Found it under menu in NetBeans IDE:

Tools > Servers > Apache Tomcat...

(Then under Connection tab, Catalina Home:)

C:\Program Files\Apache Software Foundation\Tomcat 9.0

Clearly it wasnt pointing to XAMPP application!! Changed it to:

C:\xampp\tomcat

Server in Eclipse (EE version!!):

Window > Show view > Other > Server > servers (open...)

(No servers available. Click this link ) << Click this on 'Servers' tab below Then choose Apache > Tomcat v8.0 << confirm your version
Click Next (Tomcat intallation directory:) << Search for the correct folder eg.

C:\xampp\tomcat  

Then my tomcat user settings match those in tomcat-users.xml

C:\xampp\tomcat\conf\tomcat-users.xml

My POM.xml in Netbeans:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>  <!-- dont change -->
        <!-- below must match apache-maven-3.x.x\conf\settings.xml -->
        <server>TomcatServer</server>  
        <username>admin</username>  
        <password>admin</password>
        <path></path> <!--no path: deploys to C:\xampp\tomcat\webapps\-->
    </configuration>
</plugin>

Tomcat server entry: in C:\Program Files\Apache Software Foundation\apache-maven-3.8.5\conf\settings.xml

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

Tomcat users: in C:\xampp\tomcat\conf\tomcat-users.xml

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">

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

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

I Run maven with goals (right cick project and Run maven)

install tomcat7:deploy

Before testing i ensure website is listed and running in

http://localhost:8080/manager/html/

Salute answered 19/3, 2022 at 14:40 Comment(0)
C
-1

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.

Cyndy answered 15/3, 2013 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.