Stuart McCulloch | cda5cba | 2008-02-05 05:34:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.obr.plugin; |
| 20 | |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.FileNotFoundException; |
| 24 | import java.io.FileReader; |
| 25 | import java.io.IOException; |
| 26 | import java.io.Reader; |
| 27 | |
| 28 | import org.apache.maven.model.Model; |
| 29 | import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 30 | import org.apache.maven.plugin.MojoExecutionException; |
| 31 | import org.apache.maven.project.MavenProject; |
| 32 | import org.codehaus.plexus.util.IOUtil; |
| 33 | import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Maven POM helper methods. |
| 38 | * |
| 39 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 40 | */ |
| 41 | public final class PomHelper |
| 42 | { |
| 43 | public static MavenProject readPom( File pomFile ) throws MojoExecutionException |
| 44 | { |
| 45 | Reader reader = null; |
| 46 | |
| 47 | try |
| 48 | { |
| 49 | reader = new FileReader( pomFile ); |
| 50 | MavenXpp3Reader modelReader = new MavenXpp3Reader(); |
| 51 | return new MavenProject( modelReader.read( reader ) ); |
| 52 | } |
| 53 | catch ( FileNotFoundException e ) |
| 54 | { |
| 55 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
| 56 | } |
| 57 | catch ( IOException e ) |
| 58 | { |
| 59 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
| 60 | } |
| 61 | catch ( XmlPullParserException e ) |
| 62 | { |
| 63 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
| 64 | } |
| 65 | finally |
| 66 | { |
| 67 | IOUtil.close( reader ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | public static MavenProject buildPom( String groupId, String artifactId, String version, String packaging ) |
| 73 | { |
| 74 | Model model = new Model(); |
| 75 | |
| 76 | model.setModelVersion( "4.0.0" ); |
| 77 | model.setGroupId( groupId ); |
| 78 | model.setArtifactId( artifactId ); |
| 79 | model.setVersion( version ); |
| 80 | model.setPackaging( packaging ); |
| 81 | |
| 82 | return new MavenProject( model ); |
| 83 | } |
| 84 | } |