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.io.IOException;
41  import org.apache.maven.model.Plugin;
42  import org.apache.maven.model.PluginExecution;
43  import org.apache.maven.plugin.MojoExecutionException;
44  import org.apache.maven.plugin.MojoFailureException;
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  import org.codehaus.plexus.util.FileUtils;
50  import org.codehaus.plexus.util.xml.Xpp3Dom;
51  
52  /**
53   * Reads a JAX-WS service endpoint implementation class
54   * and generates all of the portable artifacts for a JAX-WS web service.
55   */
56  @Mojo( name = "wsgen", defaultPhase = LifecyclePhase.PROCESS_CLASSES,
57          requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true )
58  public class MainWsGenMojo
59      extends AbstractWsGenMojo
60  {
61  
62      /**
63       * Specify where to place output generated classes. Use <code>xnocompile</code>
64       * to turn this off.
65       */
66      @Parameter( defaultValue = "${project.build.outputDirectory}" )
67      private File destDir;
68  
69      /**
70       * Specify where to place generated source files, keep is turned on with this option.
71       */
72      @Parameter( defaultValue = "${project.build.directory}/generated-sources/wsgen" )
73      private File sourceDestDir;
74  
75      /**
76       * Directory containing the generated wsdl files.
77       */
78      @Parameter( defaultValue = "${project.build.directory}/generated-sources/wsdl" )
79      private File resourceDestDir;
80  
81      /**
82       * Either ${build.outputDirectory} or ${build.testOutputDirectory}.
83       */
84      @Override
85      protected File getDestDir()
86      {
87          return destDir;
88      }
89  
90      @Override
91      protected File getSourceDestDir()
92      {
93          return sourceDestDir;
94      }
95  
96      @Override
97      protected File getResourceDestDir()
98      {
99          return resourceDestDir;
100     }
101 
102     @Override
103     protected File getDefaultSrcOut()
104     {
105         return new File( project.getBuild().getDirectory(), "generated-sources/wsgen" );
106     }
107 
108     @Override
109     protected File getClassesDir()
110     {
111         return new File( project.getBuild().getOutputDirectory() );
112     }
113 
114     @Override
115     public void executeJaxws()
116         throws MojoExecutionException, MojoFailureException
117     {
118         super.executeJaxws();
119         if ( genWsdl )
120         {
121             try
122             {
123                 attachWsdl();
124             }
125             catch ( IOException ex )
126             {
127                 throw new MojoExecutionException( "Failed to execute wsgen", ex );
128             }
129         }
130 
131     }
132 
133     private void attachWsdl()
134         throws IOException
135     {
136         File target = new File( project.getBuild().getDirectory() );
137         if ( !"war".equalsIgnoreCase( project.getPackaging() ) )
138         {
139             // META-INF/wsdl for jar etc packagings
140             target = new File( project.getBuild().getOutputDirectory(), "META-INF/wsdl" );
141         }
142         else
143         {
144             // WEB-INF/wsdl for war
145             String targetPath = null;
146             Plugin war = project.getBuild().getPluginsAsMap().get( "org.apache.maven.plugins:maven-war-plugin" );
147             for ( PluginExecution exec : war.getExecutions() )
148             {
149                 // check execution/configuration
150                 String s = getWebappDirectory( exec.getConfiguration() );
151                 if ( s != null )
152                 {
153                     targetPath = s;
154                     break;
155                 }
156             }
157             if ( targetPath == null )
158             {
159                 // check global plugin configuration
160                 targetPath = getWebappDirectory( war.getConfiguration() );
161             }
162             target =
163                 targetPath != null ? new File( targetPath ) : new File( target, project.getBuild().getFinalName() );
164             target = new File( target, "WEB-INF/wsdl" );
165         }
166         if ( !target.mkdirs() && !target.exists() )
167         {
168             getLog().warn( "Cannot create directory: " + target.getAbsolutePath() );
169         }
170         getLog().debug( "Packaging WSDL(s) to: " + target );
171         FileUtils.copyDirectory( getResourceDestDir(), target );
172     }
173 
174     private String getWebappDirectory( Object conf )
175     {
176         if ( conf == null )
177         {
178             return null;
179         }
180         Xpp3Dom el = ( (Xpp3Dom) conf ).getChild( "webappDirectory" );
181         return el != null ? el.getValue() : null;
182     }
183 }