title: Maven lifecycle within jenkins pipeline - how to best separate responsibilities?
body: <p>When working with jenkins 2 (declarative) pipelines and maven I always have a problem with how to organize things within the pipeline to make it resusable and flexible.</p>
  
  <p>On the one side I would like to seperate the pipepline into logical stages like:</p>
  
  <pre><code>pipeline
   {
    stages
     {
      stage('Clean') {}
      stage('Build') {}
      stage('Test') {}
      stage('Sanity check') {}
      stage('Documentation') {}
      stage('Deploy - Test') {}
      stage('Selenium tests') {}
      stage('Deploy - Production') {}
      stage('Deliver') {}
     }
   }
  </code></pre>
  
  <p>On the other hand I have maven which runs with</p>
  
  <pre><code>mvn clean deploy site
  </code></pre>
  
  <p>Simply I could split up maven to</p>
  
  <pre><code>mvn clean
  mvn deploy
  mvn site
  </code></pre>
  
  <p>But the 'deploy' includes all lifecycle phases from</p>
  
  <ul>
  <li>validate</li>
  <li>compile</li>
  <li>test</li>
  <li>package</li>
  <li>verify</li>
  <li>install</li>
  <li>deploy</li>
  </ul>
  
  <p>So I saw a lot of pipline examples which do things like</p>
  
  <pre><code>sh 'mvn clean compile'
  </code></pre>
  
  <p>and</p>
  
  <pre><code>sh 'mvn test'
  </code></pre>
  
  <p>which results in repeating the validate and compile step a second time and waste "time/resources" in this way.
  This could be resolved with doing a</p>
  
  <pre><code>sh 'mvn surefire:test'
  </code></pre>
  
  <p>instead of running the whole lifecycle again.</p>
  
  <p>So my question is - which is the best way to get a good balance between the jenkins pipline stages and the maven lifecycle?
  For me I see two ways:</p>
  
  <ol>
  <li>Split up the maven lifecycles to as much pipeline stages as possible - which will result in better jenkins user feedback (see which stage fails etc.)</li>
  <li>Let maven do everything and use the jenkins pipeline only to work with the results of maven (i.e. analyzing unit test results etc.)</li>
  </ol>
  
  <p>Or did I missunderstand something in the CI/CD practice?</p>