View Javadoc
1   package com.ericsson.research.trap;
2   
3   /*
4    * ##_BEGIN_LICENSE_##
5    * Transport Abstraction Package (trap)
6    * ----------
7    * Copyright (C) 2014 Ericsson AB
8    * ----------
9    * Redistribution and use in source and binary forms, with or without modification,
10   * are permitted provided that the following conditions are met:
11   * 
12   * 1. Redistributions of source code must retain the above copyright notice, this
13   *    list of conditions and the following disclaimer.
14   * 
15   * 2. Redistributions in binary form must reproduce the above copyright notice,
16   *    this list of conditions and the following disclaimer in the documentation
17   *    and/or other materials provided with the distribution.
18   * 
19   * 3. Neither the name of the Ericsson AB nor the names of its contributors
20   *    may be used to endorse or promote products derived from this software without
21   *    specific prior written permission.
22   * 
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32   * OF THE POSSIBILITY OF SUCH DAMAGE.
33   * ##_END_LICENSE_##
34   */
35  
36  /*
37   * Copyright (c) 2014, Ericsson AB
38   * 
39   * All rights reserved.
40   * 
41   * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
42   * following conditions are met:
43   * 
44   * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
45   * disclaimer.
46   * 
47   * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
48   * following disclaimer in the documentation and/or other materials provided with the distribution.
49   * 
50   * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
51   * products derived from this software without specific prior written permission.
52   * 
53   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
54   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
58   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60   */
61  
62  import java.io.File;
63  import java.io.FileOutputStream;
64  import java.io.FileWriter;
65  import java.io.IOException;
66  import java.io.OutputStreamWriter;
67  import java.util.List;
68  
69  import org.apache.maven.plugin.AbstractMojo;
70  import org.apache.maven.plugin.MojoExecutionException;
71  import org.apache.maven.plugin.MojoFailureException;
72  import org.apache.maven.plugins.annotations.Component;
73  import org.apache.maven.plugins.annotations.LifecyclePhase;
74  import org.apache.maven.plugins.annotations.Mojo;
75  import org.apache.maven.plugins.annotations.Parameter;
76  import org.apache.maven.plugins.annotations.ResolutionScope;
77  import org.apache.maven.project.MavenProject;
78  import org.eclipse.aether.RepositorySystem;
79  import org.eclipse.aether.RepositorySystemSession;
80  import org.eclipse.aether.repository.RemoteRepository;
81  
82  import com.ericsson.research.trap.utils.PackageScanner;
83  
84  @Mojo(configurator = "include-project-dependencies", name = "trapindex", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDirectInvocation = false)
85  public class IndexMojo extends AbstractMojo
86  {
87      /**
88       * The project currently being build.
89       * 
90       * @parameter expression="${project}"
91       * @required
92       * @readonly
93       */
94      @Parameter(required = true, readonly = true, defaultValue = "${project}")
95      private MavenProject            mavenProject;
96      
97      /**
98       * @parameter default-value="${project.build.outputDirectory}/trap-packages.txt"
99       */
100     @Parameter(defaultValue = "${project.build.outputDirectory}/trap-packages.txt")
101     private File                    targetFile;
102     
103     /**
104      * The entry point to Aether, i.e. the component doing all the work.
105      * 
106      * @required
107      * @component
108      */
109     @Component
110     private RepositorySystem        repoSystem;
111     
112     /**
113      * The current repository/network configuration of Maven.
114      * 
115      * @parameter default-value="${repositorySystemSession}"
116      * @readonly
117      */
118     @Parameter(defaultValue = "${repositorySystemSession}")
119     private RepositorySystemSession repoSession;
120     
121     /**
122      * The project's remote repositories to use for the resolution of project dependencies.
123      * 
124      * @parameter default-value="${project.remoteProjectRepositories}"
125      * @readonly
126      */
127     @Parameter(defaultValue = "${project.remoteProjectRepositories}")
128     private List<RemoteRepository>  projectRepos;
129     
130     /**
131      * The project's remote repositories to use for the resolution of plugins and their dependencies.
132      * 
133      * @parameter default-value="${project.remotePluginRepositories}"
134      * @readonly
135      */
136     @Parameter(defaultValue = "${project.remotePluginRepositories}")
137     private List<RemoteRepository>  pluginRepos;
138     
139     /**
140      * @parameter
141      */
142     @Parameter
143     private String[]                packages;
144     
145     public void execute() throws MojoExecutionException, MojoFailureException
146     {
147         FileOutputStream fos = null;
148         OutputStreamWriter osw = null;
149         
150         try
151         {
152             //System.out.println("Using class loader " + this.getClass().getClassLoader());
153             
154             File parent = this.targetFile.getParentFile();
155             
156             if (parent.exists() && !parent.isDirectory())
157                 throw new MojoExecutionException("Parent directory " + parent + " is actually a file");
158             
159             if (!parent.exists() && !parent.mkdirs())
160                 throw new MojoExecutionException("Could not create target directory " + parent);
161             
162             fos = new FileOutputStream(this.targetFile);
163             osw = new OutputStreamWriter(fos, "UTF-8");
164             
165             osw = new FileWriter(this.targetFile);
166             for (String pkg : this.packages)
167             {
168                 //System.out.println("Scanning " + pkg);
169                 Class<?>[] cs = PackageScanner.scan(pkg, this.getClass().getClassLoader());
170                 
171                 //System.out.println("Got cs: " + cs.length);
172                 
173                 for (Class<?> c : cs)
174                 {
175                     // Skip adding ourselves. The receiver don't care.
176                     if (c.getName().equals(this.getClass().getName()) || IndexConfigurator.class.getName().equals(c.getName()))
177                         continue;
178                     //System.out.println("Scanning " + c);
179                     osw.write(c.getName());
180                     osw.write("\n");
181                 }
182             }
183             
184         }
185         catch (IOException e)
186         {
187             throw new MojoExecutionException("Error while writing the packages file", e);
188         }
189         finally
190         {
191             if (osw != null)
192                 try
193                 {
194                     osw.close();
195                 }
196                 catch (IOException e)
197                 {
198                     throw new MojoFailureException("Could not write packages file", e);
199                 }
200             if (fos != null)
201                 try
202                 {
203                     fos.close();
204                 }
205                 catch (IOException e)
206                 {
207                     throw new MojoFailureException("Could not write packages file", e);
208                 }
209         }
210     }
211     
212 }