1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3    *
4    * Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
5    *
6    * Oracle licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * 
19   * This file incorporates work covered by the following copyright and
20   * permission notice:
21   *
22   * Copyright 2006 Guillaume Nodet
23   *
24   * Licensed under the Apache License, Version 2.0 (the "License");
25   * you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at
27   *
28   *      http://www.apache.org/licenses/LICENSE-2.0
29   *
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS,
32   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   * See the License for the specific language governing permissions and
34   * limitations under the License.
35   */
36  
37  package org.codehaus.mojo.jaxws;
38  
39  import java.io.File;
40  import java.util.List;
41  import java.util.stream.Collectors;
42  
43  import org.apache.maven.artifact.Artifact;
44  import org.apache.maven.plugin.MojoExecutionException;
45  import org.apache.maven.plugins.annotations.LifecyclePhase;
46  import org.apache.maven.plugins.annotations.Mojo;
47  import org.apache.maven.plugins.annotations.Parameter;
48  import org.apache.maven.plugins.annotations.ResolutionScope;
49  
50  /**
51   * Parses wsdl and binding files and generates Java code needed to access it
52   * (for tests).
53   *
54   * <p>
55   * <code>${maven.test.skip}</code> property is honored. If it is set, code generation is skipped.
56   * </p>
57   *
58   * @author Kohsuke Kawaguchi
59   */
60  @Mojo( name = "wsimport-test", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES,
61          requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
62  public class TestWsImportMojo
63      extends WsImportMojo
64  {
65  
66      /**
67       * Specify where to place output generated classes. Use <code>xnocompile</code>
68       * to turn this off.
69       */
70      @Parameter( defaultValue = "${project.build.testOutputDirectory}" )
71      private File destDir;
72  
73      /**
74       * Specify where to place generated source files, keep is turned on with this option.
75       */
76      @Parameter( defaultValue = "${project.build.directory}/generated-sources/test-wsimport" )
77      private File sourceDestDir;
78  
79      /**
80       * Specify where to generate JWS implementation file.
81       */
82      @Parameter( defaultValue = "${project.build.testSourceDirectory}" )
83      private File implDestDir;
84  
85      /**
86       * Set this to "true" to bypass code generation.
87       */
88      @Parameter( property = "maven.test.skip" )
89      private boolean skip;
90  
91      /**
92       * Either ${build.outputDirectory} or ${build.testOutputDirectory}.
93       */
94      @Override
95      protected File getDestDir()
96      {
97          return destDir;
98      }
99  
100     /**
101      * ${project.build.directory}/jaxws/wsimport/test.
102      */
103     @Override
104     protected File getSourceDestDir()
105     {
106         return sourceDestDir;
107     }
108 
109     @Override
110     protected void addSourceRoot( String sourceDir )
111     {
112         if ( !project.getTestCompileSourceRoots().contains( sourceDir ) )
113         {
114             getLog().debug( "adding test src root: " + sourceDir );
115             project.addTestCompileSourceRoot( sourceDir );
116         }
117         else
118         {
119             getLog().debug( "existing test src root: " + sourceDir );
120         }
121     }
122 
123     @Override
124     protected File getDefaultSrcOut()
125     {
126         return new File( project.getBuild().getDirectory(), "generated-sources/test-wsimport" );
127     }
128 
129     @Override
130     protected File getImplDestDir()
131     {
132         return implDestDir;
133     }
134 
135     @Override
136     protected List<String> getWSDLFileLookupClasspathElements() 
137     {
138         return project.getDependencyArtifacts().stream()
139                 .filter( a -> !Artifact.SCOPE_RUNTIME.equals(a.getScope() )
140                         && null != a.getFile() )
141                 .map( a -> a.getFile().getPath() ).collect( Collectors.toList() );
142     }
143 
144     @Override
145     public void executeJaxws()
146         throws MojoExecutionException
147     {
148         // if maven.test.skip is set test compilation is not called, so
149         // no need to generate sources/classes
150         if ( skip )
151         {
152             getLog().info( "Skipping tests, nothing to do." );
153         }
154         else
155         {
156             super.executeJaxws();
157         }
158     }
159 }