View Javadoc

1   package org.apache.maven.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import org.apache.commons.jelly.JellyContext;
21  import org.apache.maven.MavenException;
22  import org.apache.maven.MavenUtils;
23  import org.apache.maven.jelly.MavenJellyContext;
24  import org.apache.maven.project.Dependency;
25  import org.apache.maven.project.Model;
26  import org.apache.maven.project.Project;
27  import org.apache.maven.project.io.stax.MavenStaxReader;
28  import org.apache.maven.project.io.stax.MavenStaxWriter;
29  import org.codehaus.plexus.util.IOUtil;
30  
31  import java.io.BufferedWriter;
32  import java.io.File;
33  import java.io.FileOutputStream;
34  import java.io.OutputStreamWriter;
35  import java.io.StringReader;
36  import java.io.Writer;
37  import java.lang.reflect.Method;
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.Map;
41  
42  /**
43   * Rewrite a full model for publishing. Inheritence and expression will have been resolved.
44   *
45   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
46   * @version $Id: PomRewriter.java 532339 2007-04-25 12:28:56Z ltheussl $
47   */
48  public class PomRewriter
49  {
50      public static File getRewrittenPom( final Project project )
51          throws MavenException
52      {
53          final Model model = PomRewriter.getRewrittenModel( project.getFile(), project.getContext() );
54          if ( model.getModelEncoding() == null )
55              model.setModelEncoding( "UTF-8" );
56          Writer w = null;
57          try
58          {
59              final MavenStaxWriter writer = new MavenStaxWriter();
60              final File f = File.createTempFile( "maven-artifact-plugin.", null );
61              f.deleteOnExit();
62              w = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( f ), model.getModelEncoding() ) );
63              writer.write( w, model );
64              return f;
65          }
66          catch ( final Exception e )
67          {
68              throw new MavenException( "Error getting the project as a string", e );
69          }
70          finally
71          {
72              IOUtil.close( w );
73          }
74      }
75  
76      static Model getRewrittenModel( final File file, final JellyContext context )
77          throws MavenException
78      {
79          Model model;
80          try
81          {
82              // Very gross, but we don't want initialize() called
83              // A future version should use use project.getModel() and serialize that
84              Method m = MavenUtils.class.getDeclaredMethod( "getNonJellyProject", new Class[] {
85                  File.class,
86                  MavenJellyContext.class,
87                  boolean.class } );
88              m.setAccessible( true );
89              Project p = (Project) m.invoke( null, new Object[] { file, context, Boolean.TRUE } );
90              m.setAccessible( false );
91              m = MavenUtils.class.getDeclaredMethod( "getJellyProject", new Class[] { Project.class } );
92              m.setAccessible( true );
93              p = (Project) m.invoke( null, new Object[] { p } );
94              m.setAccessible( false );
95              // The rewrittenPOM must redefine dependencies versions
96              // if override properties are used
97              p.buildArtifactList();
98  
99              // now sanitize
100             p.setContext( null );
101             p.setParent( null );
102             p.setArtifacts( null );
103             p.setDependencyVerifier( null );
104             p.setExtend( null );
105 
106             final Map depProperties = new HashMap();
107             for ( final Iterator i = p.getDependencies().iterator(); i.hasNext(); )
108             {
109                 final org.apache.maven.project.Dependency d = (org.apache.maven.project.Dependency) i.next();
110                 final Map properties = d.getProperties();
111                 if ( ( properties != null ) && !properties.isEmpty() )
112                 {
113                     depProperties.put( d.getId(), properties );
114                     d.setProperties( null );
115                 }
116             }
117 
118             final String asString = p.getProjectAsString();
119 
120             final MavenStaxReader reader = new MavenStaxReader();
121             model = reader.read( new StringReader( asString ) );
122             model.setId( null );
123 
124             for ( final Iterator i = model.getDependencies().iterator(); i.hasNext(); )
125             {
126                 final Dependency d = (Dependency) i.next();
127 
128                 if ( depProperties.containsKey( d.getId() ) )
129                 {
130                     d.getProperties().putAll( (Map) depProperties.get( d.getId() ) );
131                 }
132 
133                 d.setId( null );
134 
135                 if ( ( d.getUrl() != null ) && ( d.getUrl().length() == 0 ) )
136                 {
137                     d.setUrl( null );
138                 }
139 
140                 if ( ( d.getType() != null ) && ( d.getType().length() == 0 ) )
141                 {
142                     d.setType( null );
143                 }
144 
145                 if ( ( d.getJar() != null ) && ( d.getJar().length() == 0 ) )
146                 {
147                     d.setJar( null );
148                 }
149 
150             }
151         }
152         catch ( final Exception e )
153         {
154             throw new MavenException( "Error getting the project as a string", e );
155         }
156         return model;
157     }
158 }