Categories
Java

Deploy to Tomcat with Maven

In my last post I decribed the way to build and deploy a war file to Tomcat application server with Ant.

In this post I show how to deploy the build war file to Tomcat application server with Maven.

Create inside the "ant" folder this Maven file:


    4.0.0
    deringo
    myApp
    0.0.1-SNAPSHOT
    war
    myApp
    myApp Maven deployment

    
        1.8
        8.5.33
        UTF-8
    

    
        
            org.apache.tomcat
            tomcat-catalina
            ${tomcat.version}
            provided
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    ${java.version}
                    ${java.version}
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                3.2.2
                
                    false
                    myApp
                    myApp
                
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    http://myAppServer:7011/manager/text
                    tomcat
                    tomcat
                    dist/myApp.war
                    /myApp
                    true
                
            
        
    

Execute with goal: tomcat7:deploy

Use Properties file

In my ANT file I have used a properties file for tomcat username/password etc. In my Maven script I want also to use this properties file.

Unfortunatly Maven can not handle property files out of the box. But there is a Plugin we can use:

        
            org.codehaus.mojo
            properties-maven-plugin
            1.0-alpha-1
            
                
                    initialize
                    
                        read-project-properties
                    
                    
                        
                            tomcat.properties
                        
                    
                
            
        

This is the same, as writing this directly into pom.xml:


    tomcat
    tomcat

The complete pom.xml with properties:


  4.0.0
  deringo
  myApp
  0.0.1-SNAPSHOT
  war
  myApp
  myApp Maven deployment

  
    1.8
    8.5.33
    UTF-8
  

  
    
      org.apache.tomcat
      tomcat-catalina
      ${tomcat.version}
      provided
    
  

  
    
      
        org.codehaus.mojo
        properties-maven-plugin
        1.0-alpha-1
        
          
            initialize
            
              read-project-properties
            
            
              
                tomcat.properties
              
            
          
        
      
      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.8.0
        
          ${java.version}
          ${java.version}
        
      
      
        org.apache.maven.plugins
        maven-war-plugin
        3.2.2
        
          false
          myApp
          myApp
        
      
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
        
          ${tomcat.manager.url}
          ${tomcat.username}
          ${tomcat.password}
          dist/myApp.war
          ${webapp.name}
          true
        
      
    
  

Update 2021-03-24

I tried the configuration in a project but got this error:

Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)

I solved the error adding a pluginManagement tag:


    
        
             ... 
             ... 
                  ....
        
    

But this solution seems more like a workaround when reading the discussion on stackoverflow.
...need to search further...

Use in Jenkins

I have not tried it, but it should be possible to set an environment variable in Jenkins build step to execute with a configurable filename.

For example TOMCAT_PROPERTIES=jenkins-home/secret/tomcat-dev.properties.
And we can use it in Maven script this way:

            
              
                ${env.TOMCAT_PROPERTIES}
              
            

Leave a Reply

Your email address will not be published. Required fields are marked *