Changeset 752

Show
Ignore:
Timestamp:
13/11/08 15:04:54 (2 months ago)
Author:
bruno
Message:

Webapp plugin:

  • removing unnecessary dependencies from pom (I think)
  • added a KauriPackageMojo?, sharing a lot of code with KauriWebappMojo?, which is the basis for the upcoming kauri-native application packaging format

This project will hence also be renamed to something more generic.

!!! Attention: the goal name for the webapp mojo changed from 'generate' to 'webapp'.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/servlet/kauri-webapp-plugin/pom.xml

    r597 r752  
    1616   
    1717  <dependencies> 
    18     <dependency> 
    19       <groupId>org.kauriproject</groupId> 
    20       <artifactId>kauri-runtime</artifactId> 
    21     </dependency> 
    22     <dependency> 
    23       <groupId>org.kauriproject</groupId> 
    24       <artifactId>kauri-runtime-servlet</artifactId> 
    25       <scope>runtime</scope> 
    26     </dependency> 
    2718    <dependency> 
    2819      <groupId>org.apache.maven</groupId> 
  • trunk/tools/servlet/kauri-webapp-plugin/src/main/java/org/kauriproject/tools/plugin/webapp/KauriWebappMojo.java

    r743 r752  
    11package org.kauriproject.tools.plugin.webapp; 
    22 
    3 import org.apache.maven.plugin.AbstractMojo; 
    43import org.apache.maven.plugin.MojoExecutionException; 
    54import org.apache.maven.plugin.MojoFailureException; 
    6 import org.apache.maven.project.MavenProjectBuilder; 
    75import org.apache.maven.project.MavenProject; 
    86import org.apache.maven.project.artifact.MavenMetadataSource; 
    9 import org.apache.maven.artifact.factory.ArtifactFactory; 
    107import org.apache.maven.artifact.Artifact; 
    11 import org.apache.maven.artifact.metadata.ArtifactMetadataSource; 
    128import org.apache.maven.artifact.resolver.ArtifactResolutionResult; 
    13 import org.apache.maven.artifact.resolver.ArtifactResolver; 
    149import org.apache.maven.artifact.resolver.filter.ArtifactFilter; 
    15 import org.apache.maven.artifact.repository.ArtifactRepository; 
    16 import org.w3c.dom.Document; 
    17 import org.w3c.dom.NodeList; 
    18 import org.w3c.dom.Element; 
    19 import org.xml.sax.SAXException; 
    2010 
    21 import javax.xml.xpath.XPathFactory; 
    22 import javax.xml.xpath.XPathConstants; 
    23 import javax.xml.xpath.XPathExpressionException; 
    24 import javax.xml.parsers.DocumentBuilderFactory; 
    25 import javax.xml.parsers.ParserConfigurationException; 
    2611import java.util.*; 
    27 import java.util.zip.ZipFile; 
    28 import java.util.zip.ZipEntry; 
    29 import java.util.regex.Matcher; 
    3012import java.io.*; 
    31 import java.nio.channels.FileChannel; 
    3213 
    3314 
     
    3819 * but rather gets all its information from the kauri.xml file. 
    3920 * 
    40  * @goal generate 
     21 * @goal webapp 
    4122 * @requiresDependencyResolution runtime 
    4223 * @description Genenerate a Kauri classloader file for a module. 
    4324 */ 
    44 public class KauriWebappMojo extends AbstractMojo { 
    45     /** 
    46      * Location of the kauri.xml. 
    47      * 
    48      * @parameter 
    49      * @required 
    50      */ 
    51     protected String kauriConfigLocation; 
    52  
    53     /** 
    54      * Maven Project Builder component. 
    55      * 
    56      * @component 
    57      */ 
    58     protected MavenProjectBuilder projectBuilder; 
    59  
    60     /** 
    61      * Maven Artifact Factory component. 
    62      * 
    63      * @component 
    64      */ 
    65     protected ArtifactFactory artifactFactory; 
    66  
    67  
    68     /** 
    69      * Remote repositories used for the project. 
    70      * 
    71      * @parameter expression="${project.remoteArtifactRepositories}" 
    72      * @required 
    73      * @readonly 
    74      */ 
    75     protected List remoteRepositories; 
    76  
    77     /** 
    78      * Local Repository. 
    79      * 
    80      * @parameter expression="${localRepository}" 
    81      * @required 
    82      * @readonly 
    83      */ 
    84     protected ArtifactRepository localRepository; 
    85  
    86     /** 
    87      * Artifact Resolver component. 
    88      * 
    89      * @component 
    90      */ 
    91     protected ArtifactResolver resolver; 
    92  
    93     /** 
    94      * Artifact Resolver component. 
    95      * 
    96      * @component 
    97      */ 
    98     protected ArtifactMetadataSource metadataSource; 
    99  
    100     /** 
    101      * The Kauri version to employ. 
    102      * 
    103      * @parameter 
    104      */ 
    105     protected String kauriVersion; 
     25public class KauriWebappMojo extends AbstractPackageMojo { 
    10626 
    10727    /** 
     
    11939    protected String webXmlLocation; 
    12040 
    121     private XPathFactory xpathFactory = XPathFactory.newInstance(); 
    122  
    12341    public void execute() throws MojoExecutionException, MojoFailureException { 
    124         if (kauriVersion == null) 
    125             determineKauriVersion(); 
     42        init(); 
    12643 
    12744        deleteOldWebapp(); 
     
    14865        } 
    14966 
    150         createRepository(allArtifacts); 
     67        createRepository(allArtifacts, webappDirectory + "/WEB-INF/repository/"); 
    15168 
    15269        // 
     
    16077        // 
    16178        createWebXml(); 
    162     } 
    163  
    164     private Set<Artifact> getModuleArtifactsFromKauriConfig() throws MojoExecutionException { 
    165         Document kauriConfigDoc; 
    166         try { 
    167             File kauriConfigFile = new File(kauriConfigLocation); 
    168             FileInputStream fis = null; 
    169             try { 
    170                 fis = new FileInputStream(kauriConfigFile); 
    171                 kauriConfigDoc = parse(fis); 
    172             } finally { 
    173                 if (fis != null) 
    174                     fis.close(); 
    175             } 
    176         } catch (Exception e) { 
    177             throw new MojoExecutionException("Error reading kauri XML configuration from " + kauriConfigLocation, e); 
    178         } 
    179  
    180         return getArtifacts(kauriConfigDoc, "/runtime/modules/artifact", "Kauri configuration"); 
    181    } 
    182  
    183  
    184     private Document parse(InputStream is) throws ParserConfigurationException, IOException, SAXException { 
    185         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    186         dbf.setNamespaceAware(true); 
    187         return dbf.newDocumentBuilder().parse(is); 
    188     } 
    189  
    190     private void determineKauriVersion() throws MojoExecutionException { 
    191         String pomPropsPath = "META-INF/maven/org.kauriproject/kauri-webapp-plugin/pom.properties"; 
    192         InputStream is = KauriWebappMojo.class.getClassLoader().getResourceAsStream(pomPropsPath); 
    193         if (is == null) { 
    194             throw new MojoExecutionException("Could not find the resource containing the Kauri version information at " + pomPropsPath); 
    195         } 
    196  
    197         Properties pomProps = new Properties(); 
    198         try { 
    199             pomProps.load(is); 
    200         } catch (IOException e) { 
    201             throw new MojoExecutionException("Error reading pom properties from " + pomPropsPath, e); 
    202         } 
    203         this.kauriVersion = pomProps.getProperty("version"); 
    204     } 
    205  
    206     private void createRepository(Set<Artifact> artifacts) throws MojoExecutionException { 
    207         getLog().info("Creating the webapp-embedded artifact repository containing " + artifacts.size() + " artifacts."); 
    208         for (Artifact artifact : artifacts) { 
    209             String groupPath = artifact.getGroupId().replaceAll("\\.", Matcher.quoteReplacement("/")); 
    210             File targetDir = new File(webappDirectory + "/WEB-INF/repository/" + groupPath + "/" 
    211                     + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"); 
    212             targetDir.mkdirs(); 
    213             String fileName = artifact.getArtifactId() + "-" + artifact.getVersion(); 
    214             if(artifact.hasClassifier()) { 
    215                 fileName += "-" + artifact.getClassifier(); 
    216             } 
    217             copyFile(artifact.getFile(), new File(targetDir, fileName + ".jar")); 
    218         } 
    219     } 
    220  
    221     private void copyFile(File fromFile, File toFile) throws MojoExecutionException { 
    222         try { 
    223             FileInputStream srcFis = new FileInputStream(fromFile); 
    224             FileOutputStream destFos = new FileOutputStream(toFile); 
    225             FileChannel srcChannel = srcFis.getChannel(); 
    226             FileChannel dstChannel = destFos.getChannel(); 
    227             dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 
    228             srcChannel.close(); 
    229             dstChannel.close(); 
    230             srcFis.close(); 
    231             destFos.close(); 
    232         } catch (Throwable t) { 
    233             throw new MojoExecutionException("Failed to copy file from " + fromFile + " to " + toFile); 
    234         } 
    235     } 
    236  
    237     private Set<Artifact> getClassPathArtifacts(Artifact moduleArtifact) throws MojoExecutionException { 
    238         String entryPath = "KAURI-INF/classloader.xml"; 
    239         ZipFile zipFile = null; 
    240         InputStream is = null; 
    241         Document classLoaderDocument; 
    242         try { 
    243             zipFile = new ZipFile(moduleArtifact.getFile()); 
    244             ZipEntry zipEntry = zipFile.getEntry(entryPath); 
    245             if (zipEntry == null) { 
    246                 getLog().debug("No " + entryPath + " found in " + moduleArtifact); 
    247                 return Collections.emptySet(); 
    248             } else { 
    249                 is = zipFile.getInputStream(zipEntry); 
    250                 classLoaderDocument = parse(is); 
    251             } 
    252         } catch (Exception e) { 
    253             throw new MojoExecutionException("Error reading " + entryPath + " from " + moduleArtifact, e); 
    254         } finally { 
    255             if (is != null) 
    256                 try { is.close(); } catch (Exception e) { /* ignore */ } 
    257             if (zipFile != null) 
    258                 try { zipFile.close(); } catch (Exception e) { /* ignore */ } 
    259         } 
    260  
    261         return getArtifacts(classLoaderDocument, "/classloader/classpath/artifact", "classloader.xml from module " + moduleArtifact); 
    262     } 
    263  
    264     private Set<Artifact> getArtifacts(Document configDoc, String artifactXPath, String sourceDescr) throws MojoExecutionException { 
    265         Set<Artifact> artifacts = new HashSet<Artifact>(); 
    266         NodeList nodeList; 
    267         try { 
    268             nodeList = (NodeList)xpathFactory.newXPath().evaluate(artifactXPath, configDoc, XPathConstants.NODESET); 
    269         } catch (XPathExpressionException e) { 
    270             throw new MojoExecutionException("Error resolving XPath expression " + artifactXPath + " on " + sourceDescr); 
    271         } 
    272         for (int i = 0; i < nodeList.getLength(); i++) { 
    273             Element el = (Element)nodeList.item(i); 
    274             String groupId = el.getAttribute("groupId"); 
    275             String artifactId = el.getAttribute("artifactId"); 
    276             String version = el.getAttribute("version"); 
    277             String classifier = el.getAttribute("classifier"); 
    278             if (version.equals("") && groupId.startsWith("org.kauriproject")) 
    279                 version = kauriVersion; 
    280             if (classifier.equals("")) 
    281                 classifier = null; 
    282  
    283             Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, "jar", classifier); 
    284             if (!artifacts.contains(artifact)) { 
    285                 try { 
    286                     resolver.resolve(artifact, remoteRepositories, localRepository); 
    287                 } catch (Exception e) { 
    288                     throw new MojoExecutionException("Error resolving artifact listed in " + sourceDescr + ": " + artifact, e); 
    289                 } 
    290                 artifacts.add(artifact); 
    291             } 
    292         } 
    293  
    294         return artifacts; 
    29579    } 
    29680 
     
    335119    } 
    336120 
    337     private void deleteDirectory(File dir) throws IOException { 
    338         if (!dir.exists()) 
    339             return; 
    340         File files[] = dir.listFiles(); 
    341         for (File file : files) { 
    342             if (file.isDirectory()) { 
    343                 deleteDirectory(file); 
    344             } else { 
    345                 boolean deleted = file.delete(); 
    346                 if (!deleted) 
    347                     throw new IOException("Unable to delete file " + file.getAbsolutePath()); 
    348             } 
    349         } 
    350  
    351         boolean deleted = dir.delete(); 
    352         if (!deleted) 
    353             throw new IOException("Unable to delete directory " + dir.getAbsolutePath()); 
    354     } 
    355  
    356121    private void createWebInfLib() throws MojoExecutionException { 
    357122        getLog().info("Creating the WEB-INF/lib dir.");