FELIX-475: refactor install-file goal to match with the one from the install plugin

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@618550 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/AbstractFileMojo.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/AbstractFileMojo.java
new file mode 100644
index 0000000..ddd6c1d
--- /dev/null
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/AbstractFileMojo.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.obr.plugin;
+
+
+import java.io.File;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+
+/**
+ * Base class for the command-line install-file and deploy-file goals.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public abstract class AbstractFileMojo extends AbstractMojo
+{
+    /**
+     * GroupId of the bundle. Retrieved from POM file if specified.
+     *
+     * @parameter expression="${groupId}"
+     */
+    private String groupId;
+
+    /**
+     * ArtifactId of the bundle. Retrieved from POM file if specified.
+     *
+     * @parameter expression="${artifactId}"
+     */
+    private String artifactId;
+
+    /**
+     * Version of the bundle. Retrieved from POM file if specified.
+     *
+     * @parameter expression="${version}"
+     */
+    private String version;
+
+    /**
+     * Packaging type of the bundle. Retrieved from POM file if specified.
+     *
+     * @parameter expression="${packaging}"
+     */
+    private String packaging;
+
+    /**
+     * Classifier type of the bundle. Defaults to none.
+     *
+     * @parameter expression="${classifier}"
+     */
+    private String classifier;
+
+    /**
+     * Location of an existing POM file.
+     *
+     * @parameter expression="${pomFile}"
+     */
+    private File pomFile;
+
+    /**
+     * Bundle file, defaults to the artifact in the local Maven repository.
+     *
+     * @parameter expression="${file}"
+     */
+    protected File file;
+
+    /**
+     * Optional XML file describing additional requirements and capabilities.
+     * 
+     * @parameter expression="${obrXml}"
+     */
+    protected String obrXml;
+
+    /**
+     * Component factory for Maven artifacts
+     * 
+     * @component
+     */
+    private ArtifactFactory m_factory;
+
+
+    /**
+     * @return project based on command-line settings, with bundle attached
+     * @throws MojoExecutionException
+     */
+    public MavenProject getProject() throws MojoExecutionException
+    {
+        final MavenProject project;
+        if ( pomFile != null && pomFile.exists() )
+        {
+            project = PomHelper.readPom( pomFile );
+
+            groupId = project.getGroupId();
+            artifactId = project.getArtifactId();
+            version = project.getVersion();
+            packaging = project.getPackaging();
+        }
+        else
+        {
+            project = PomHelper.buildPom( groupId, artifactId, version, packaging );
+        }
+
+        if ( groupId == null || artifactId == null || version == null || packaging == null )
+        {
+            throw new MojoExecutionException( "Missing group, artifact, version, or packaging information" );
+        }
+
+        Artifact bundle = m_factory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
+        project.setArtifact( bundle );
+
+        return project;
+    }
+}
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
index cf4d0a0..fb29573 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
@@ -31,7 +31,7 @@
 
 
 /**
- * Installs bundle details in the local OBR repository
+ * Installs bundle details in the local OBR repository (life-cycle goal)
  * 
  * @goal install
  * @phase install
@@ -81,12 +81,12 @@
             String mavenRepository = localRepository.getBasedir();
 
             URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
-            URI obrXml = ObrUtils.findObrXml( project.getResources() );
+            URI obrXmlFile = ObrUtils.findObrXml( project.getResources() );
             URI bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
 
             Config userConfig = new Config();
 
-            update = new ObrUpdate( repositoryXml, obrXml, project, bundleJar, mavenRepository, userConfig, log );
+            update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
 
             update.updateRepository();
         }
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
index 09f9c2e..c833fd5 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -19,146 +19,72 @@
 package org.apache.felix.obr.plugin;
 
 
-import java.io.File;
 import java.net.URI;
 
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 
 
 /**
- * Install bundle metadata to local OBR (command-line goal).
+ * Installs bundle details in the local OBR repository (command-line goal)
  * 
- * @goal install-file
  * @requiresProject false
+ * @goal install-file
  * @phase install
  * 
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class ObrInstallFile extends AbstractMojo
+public final class ObrInstallFile extends AbstractFileMojo
 {
     /**
-     * Component factory for Maven artifacts
+     * OBR Repository.
      * 
-     * @component
+     * @parameter expression="${obrRepository}"
      */
-    private ArtifactFactory m_factory;
+    private String obrRepository;
 
     /**
-     * The local Maven repository.
+     * Local Repository.
      * 
      * @parameter expression="${localRepository}"
      * @required
      * @readonly
      */
-    private ArtifactRepository m_localRepo;
-
-    /**
-     * path to the repository.xml.
-     * 
-     * @parameter expression="${repository-path}" alias="repository-path"
-     */
-    private String m_repositoryPath;
-
-    /**
-     * Artifact Id.
-     * @description symbolic name define by the user
-     * @parameter expression="${artifactId}"
-     */
-    private String m_artifactId;
-
-    /**
-     * Group Id.
-     * @description groupId define by the user
-     * @parameter expression="${groupId}"
-     */
-    private String m_groupId;
-
-    /**
-     * Version.
-     * @description version define by the user
-     * @parameter expression="${version}"
-     */
-    private String m_version;
-
-    /**
-     * Packaging.
-     * @description packaging define by the user
-     * @parameter expression="${packaging}"
-     */
-    private String m_packaging;
-
-    /**
-     * OBR File.
-     * @description obr file define by the user
-     * @parameter expression="${obr-file}" alias="obr-file"
-     */
-    private String m_obrFile;
+    private ArtifactRepository localRepository;
 
 
-    /**
-     * main method for this goal.
-     * @implements org.apache.maven.plugin.Mojo.execute 
-     * @throws MojoExecutionException if the plugin failed
-     */
     public void execute() throws MojoExecutionException
     {
-        MavenProject project = new MavenProject();
-        project.setArtifactId( m_artifactId );
-        project.setGroupId( m_groupId );
-        project.setVersion( m_version );
-        project.setPackaging( m_packaging );
+        MavenProject project = getProject();
 
-        if ( m_groupId == null )
+        if ( "NONE".equalsIgnoreCase( obrRepository ) )
         {
-            getLog().error( "-DgroupId=VALUE is required" );
-            return;
-        }
-        if ( m_artifactId == null )
-        {
-            getLog().error( "-Dartifactid=VALUE is required" );
-            return;
-        }
-        if ( m_version == null )
-        {
-            getLog().error( "-Dversion=VALUE is required" );
-            return;
-        }
-        if ( m_packaging == null )
-        {
-            getLog().error( "-Dpackaging=VALUE is required" );
             return;
         }
 
-        // locate the obr.xml file
-        URI obrXml = ObrUtils.toFileURI( m_obrFile );
-        if ( null == obrXml )
+        Log log = getLog();
+        ObrUpdate update;
+
+        String mavenRepository = localRepository.getBasedir();
+
+        URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
+        URI obrXmlFile = ObrUtils.toFileURI( obrXml );
+        URI bundleJar;
+
+        if ( null == file )
         {
-            getLog().info( "obr.xml is not present, use default" );
+            bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
+        }
+        else
+        {
+            bundleJar = file.toURI();
         }
 
-        Artifact bundleArtifact = m_factory.createBuildArtifact( m_groupId, m_artifactId, m_version, m_packaging );
-
-        // get the path to local maven repository
-        String mavenRepository = m_localRepo.getBasedir();
-
-        URI repoXml = ObrUtils.findRepositoryXml( mavenRepository, m_repositoryPath );
-        URI bundleJar = ObrUtils.findBundleJar( m_localRepo, bundleArtifact );
-
-        if ( !new File( bundleJar ).exists() )
-        {
-            getLog().error( "file not found in local repository: " + bundleJar );
-            return;
-        }
-
-        // use default configuration
         Config userConfig = new Config();
 
-        ObrUpdate update = new ObrUpdate( repoXml, obrXml, project, bundleJar, null, userConfig, getLog() );
+        update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
 
         update.updateRepository();
     }
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/PomHelper.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/PomHelper.java
new file mode 100644
index 0000000..7d81e09
--- /dev/null
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/PomHelper.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.obr.plugin;
+
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+
+/**
+ * Maven POM helper methods.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public final class PomHelper
+{
+    public static MavenProject readPom( File pomFile ) throws MojoExecutionException
+    {
+        Reader reader = null;
+
+        try
+        {
+            reader = new FileReader( pomFile );
+            MavenXpp3Reader modelReader = new MavenXpp3Reader();
+            return new MavenProject( modelReader.read( reader ) );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+    }
+
+
+    public static MavenProject buildPom( String groupId, String artifactId, String version, String packaging )
+    {
+        Model model = new Model();
+
+        model.setModelVersion( "4.0.0" );
+        model.setGroupId( groupId );
+        model.setArtifactId( artifactId );
+        model.setVersion( version );
+        model.setPackaging( packaging );
+
+        return new MavenProject( model );
+    }
+}