Search code examples
mavenjenkinsillegalargumentexceptionautodeploytomcat-manager

Jenkins auto deploying failed, when all settings are right. -- error in mapperLocations, Spring (illegalArgumentException, TomcatManagerException)


I want to share some Jenkins auto-deployment issue solutions, that I could not find on the internet including here.
But I'm almost a freshman in StackOverflow, so I'm just here to ask myself and answer myself right away.
// I have tried to share the solution public on GitHub issue, I could not use it well, too.
// asking to be excused...


I'm using Spring MVC 4.3.9, connected to CI/CD. // GitHub branch -> Jenkins -> TestServer (Tomcat 8.5)

I allowed all the IP addresses, made and set the right Tomcat Manager account both in Jenkins credentials menu and tomcat. But the same error always occurs -- TomcatManagerException and also the illegal argument exception (in the full log from Jenkins by maven build option, -X -e, not sure). // Same results, with both Deploy war plugin and SSH_publish plugin.

Here is the Jenkins console output. (I don't think this one is the full log.)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.895 s
[INFO] Finished at: ~~~Time~~~
[INFO] ------------------------------------------------------------------------
[DeployPublisher][INFO] Attempting to deploy 1 war file(s)
[DeployPublisher][INFO] Deploying ~~YourDIR&WAR~~ to container Tomcat 8.x Remote with context ~~YourContextName~~
  Redeploying [ ~~YourDIR&WAR~~]
  Undeploying  ~~YourDIR&WAR~~]
  Deploying [ ~~YourDIR&WAR~~]
ERROR: Build step failed with exception
org.codehaus.cargo.container.ContainerException: Failed to deploy [ ~~YourDIR&WAR~~]
    at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:104)
    at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:180)
    at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:81)
    at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:167)
    at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:136)
    at hudson.FilePath.act(FilePath.java:1075)
    at hudson.FilePath.act(FilePath.java:1058)
    at hudson.plugins.deploy.CargoContainerAdapter.redeployFile(CargoContainerAdapter.java:133)
    at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeployFile(PasswordProtectedAdapterCargo.java:95)
    at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:113)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:78)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1905)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The Tomcat Manager responded "FAIL - Deployed application at context path [~~YourContextName~~] but context failed to start
" instead of the expected "OK" message
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:721)
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deployImpl(TomcatManager.java:767)
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deploy(TomcatManager.java:312)
    at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:99)
    ... 19 more
org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The Tomcat Manager responded "FAIL - Deployed application at context path [~~YourContextName~~] but context failed to start
" instead of the expected "OK" message
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:721)
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deployImpl(TomcatManager.java:767)
    at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deploy(TomcatManager.java:312)
    at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:99)
    at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:180)
    at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:81)
    at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:167)
    at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:136)
    at hudson.FilePath.act(FilePath.java:1075)
    at hudson.FilePath.act(FilePath.java:1058)
    at hudson.plugins.deploy.CargoContainerAdapter.redeployFile(CargoContainerAdapter.java:133)
    at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeployFile(PasswordProtectedAdapterCargo.java:95)
    at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:113)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:78)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1905)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
Build step 'Deploy war/ear to a container' marked build as failure
Finished: FAILURE


* I also got illegal argument exception in maven console, in mapperLocations setting.
===> This is the key


Solution

  •     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="org.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://yourDBurl:yourport/yourDBname" />
            <property name="username" value="youraccountname" />
            <property name="password" value="youraccountpassword" />
        </bean>
    
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath:my/package/mapper/*.xml" />
        </bean>
    <!--
    // This is a part of the root-context.xml
    // in `src/main/java/my/package/mapper`, there are XML mapper files. // "src/main/java"
    // -- In my case, the mappers were in the classpath `src/main/java`
    -->
    

    This was my original mapper setting (causing the error). This setting just worked all fine when I run the Spring project on the local server. Also in the case that I exported war file on eclipse then deploy the war with my own hand to the server manually. Also in the case that I exported war file on eclipse then deploy the war with Tomcat Manager to the server manually.
    ** But only not available on Jenkins. Maven build was always successful but only in deployment. **

    * Solution * => I have changed the mapper setting in the Spring project, and migrated mappers from src/main/java to src/main/resources => The cause of the error I have guessed is that Jenkins cannot read, <property name="mapperLocations" value="classpath:my/package/mapper/*.xml" />, this configuration. => Same JDBC config, I only changed the mybatis setting like this.

    <!-- with same jdbc config -->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="configLocation" value="classpath:mybatis/mybatis-configuration.xml" />
       </bean>
    <!--
    // This is a part of the root-context.xml
    // NOW in `src/main/resources/my/package/mapper`, there are XML mapper files. // "src/main/resources"
    -->
    


    => In src/main/resources/mybatis-configuration.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <typeAliases>
            <!-- <typeAlias alias="yourDTO" type="my.package.dto.yourDTO"></typeAlias> -->
        </typeAliases>
        <mappers>
            <mapper resource="mybatis/mapper/yourmapper.xml" />
            <!-- mappers, and so on -->
        </mappers>
    </configuration>
    


    => Put the mybatis mappers in the directory, src/main/resources/mapper. Write yourmapper.xml like usual mybatis mapper XML file.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="my.package.dto.yourDTO">
        <!-- your mybatis tag with SQL query -->
    </mapper>
    


    P.S. -- I just have guessed that Jenkins cannot read the mappers in src/main/java. -- I haven't actually understood or analyze the open-source code of Jenkins. -- So the cause I pointed out might be wrong, but at least, the auto-deployment with Jenkins was available, in my mysterious case. -- I hope this might be helpful.