View Javadoc
1   package org.codehaus.mojo.gwt;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Vector;
26  
27  import org.apache.maven.archiver.MavenArchiveConfiguration;
28  import org.apache.maven.archiver.MavenArchiver;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.LifecyclePhase;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.codehaus.plexus.archiver.Archiver;
36  import org.codehaus.plexus.archiver.jar.JarArchiver;
37  
38  
39  /**
40   * Add GWT java source code and module descriptor as resources to project jar. Alternative
41   * to gwt:resources for better Eclipse projects synchronization.
42   * 
43   * @author <a href="mailto:vlads@pyx4j.com">Vlad Skarzhevskyy</a>
44   * @deprecated use maven-source-plugin and sources classifier dependencies instead
45   */
46  @Deprecated
47  @Mojo(name = "source-jar", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
48  public class GwtSourcesJarMojo
49      extends GwtResourcesBaseMojo
50  {
51  
52      /**
53       * Name of the generated JAR.
54       */
55      @Parameter(alias = "jarName", property = "jar.finalName", defaultValue = "${project.build.finalName}", required = true)
56      private String finalName;
57  
58      @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
59      private File outputDirectory;
60  
61      /**
62       * The Jar archiver.
63       */
64      @Component(role = Archiver.class, hint="jar")
65      private JarArchiver jarArchiver;
66  
67      /**
68       * The archive configuration to use. See <a
69       * href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
70       */
71      @Parameter
72      private final MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
73  
74      /**
75       * {@inheritDoc}
76       * 
77       * @see org.apache.maven.plugin.Mojo#execute()
78       */
79      public void execute()
80          throws MojoExecutionException, MojoFailureException
81      {
82          File jarFile = new File( outputDirectory, finalName + ".jar" );
83          File origJarFile = new File( outputDirectory, finalName + "-b4gwt.jar" );
84          if ( origJarFile.exists() )
85          {
86              if ( !origJarFile.delete() )
87              {
88                  throw new MojoExecutionException( "Error removing " + origJarFile );
89              }
90          }
91          if ( !jarFile.renameTo( origJarFile ) )
92          {
93              throw new MojoExecutionException( "Error renaming " + jarFile + " to " + origJarFile );
94          }
95  
96          MavenArchiver archiver = new MavenArchiver();
97  
98          archiver.setArchiver( jarArchiver );
99          archiver.setOutputFile( jarFile );
100         archive.setForced( false );
101         // It is already created by maven-jar-plugin
102         archive.setAddMavenDescriptor( false );
103 
104         Collection<ResourceFile> files = getAllResourceFiles();
105         try
106         {
107 
108             // Avoid annoying messages in log "com/package/Ccc.java already added, skipping"
109             List<String> jarExcludes = new Vector<String>();
110 
111             // Add Sources first since they may already be present in jar from previous run and changed.
112             for ( ResourceFile file : files )
113             {
114                 jarArchiver.addFile( new File( file.basedir, file.fileRelativeName ), file.fileRelativeName );
115                 jarExcludes.add( file.fileRelativeName );
116             }
117 
118             // Add the context of original jar excluding resources that we just added base on GWT descriptors
119             jarArchiver.addArchivedFileSet( origJarFile, null, jarExcludes.toArray( new String[jarExcludes.size()] ) );
120 
121             archiver.createArchive( getProject(), archive );
122         }
123         catch ( Exception e )
124         {
125             throw new MojoExecutionException( "Error assembling JAR", e );
126         }
127 
128     }
129 
130 }