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  package org.codehaus.mojo.jaxws;
37  
38  import java.io.File;
39  
40  import org.apache.maven.plugin.MojoExecutionException;
41  import org.apache.maven.plugin.MojoFailureException;
42  import org.apache.maven.plugins.annotations.LifecyclePhase;
43  import org.apache.maven.plugins.annotations.Mojo;
44  import org.apache.maven.plugins.annotations.Parameter;
45  import org.apache.maven.plugins.annotations.ResolutionScope;
46  
47  /**
48   * Reads a JAX-WS service endpoint implementation class
49   * and generates all of the portable artifacts for a JAX-WS web service
50   * (into the generate test source directory).
51   *
52   * <p>
53   * <code>${maven.test.skip}</code> property is honored. If it is set, code generation is skipped.
54   * </p>
55   *
56   */
57  @Mojo( name = "wsgen-test", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES,
58          requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
59  public class TestWsGenMojo
60      extends AbstractWsGenMojo
61  {
62  
63      /**
64       * Specify where to place output generated classes. Use <code>xnocompile</code>
65       * to turn this off.
66       */
67      @Parameter( defaultValue = "${project.build.testOutputDirectory}" )
68      private File destDir;
69  
70      /**
71       * Specify where to place generated source files, keep is turned on with this option.
72       */
73      @Parameter( defaultValue = "${project.build.directory}/generated-sources/test-wsgen" )
74      private File sourceDestDir;
75  
76      /**
77       * Directory containing the generated wsdl files.
78       */
79      @Parameter( defaultValue = "${project.build.directory}/generated-sources/test-wsdl" )
80      private File resourceDestDir;
81  
82      /**
83       * Set this to "true" to bypass code generation.
84       */
85      @Parameter( property = "maven.test.skip" )
86      private boolean skip;
87  
88      /**
89       * Either ${build.outputDirectory} or ${build.testOutputDirectory}.
90       */
91      @Override
92      protected File getDestDir()
93      {
94          return destDir;
95      }
96  
97      /**
98       * ${project.build.directory}/generated-sources/test-wsgen.
99       */
100     @Override
101     protected File getSourceDestDir()
102     {
103         return sourceDestDir;
104     }
105 
106     @Override
107     protected void addSourceRoot( String sourceDir )
108     {
109         if ( !project.getTestCompileSourceRoots().contains( sourceDir ) )
110         {
111             getLog().debug( "adding test src root: " + sourceDir );
112             project.addTestCompileSourceRoot( sourceDir );
113         }
114         else
115         {
116             getLog().debug( "existing test src root: " + sourceDir );
117         }
118     }
119 
120     @Override
121     protected File getResourceDestDir()
122     {
123         return resourceDestDir;
124     }
125 
126     @Override
127     protected File getDefaultSrcOut()
128     {
129         return new File( project.getBuild().getDirectory(), "generated-sources/test-wsgen" );
130     }
131 
132     @Override
133     protected File getClassesDir()
134     {
135         return new File( project.getBuild().getTestOutputDirectory() );
136     }
137 
138     @Override
139     protected String getExtraClasspath()
140     {
141         String cp = super.getExtraClasspath();
142         StringBuilder buf = new StringBuilder();
143         int i = cp.indexOf( File.pathSeparatorChar );
144         buf.append( i > 0 ? cp.substring( 0, i ) : cp );
145         buf.append( File.pathSeparatorChar );
146         buf.append( project.getBuild().getOutputDirectory() );
147         if ( i > 0 && cp.substring( i ).length() > 0 )
148         {
149             buf.append( cp.substring( i ) );
150         }
151         return buf.toString();
152     }
153 
154     @Override
155     public void executeJaxws()
156         throws MojoExecutionException, MojoFailureException
157     {
158         // if maven.test.skip is set test compilation is not called, so
159         // no need to generate sources/classes
160         if ( skip )
161         {
162             getLog().info( "Skipping tests, nothing to do." );
163         }
164         else
165         {
166             super.executeJaxws();
167         }
168     }
169 }