[MDEPLOY-178] Use information provided in pom.xml of JAR

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1649209 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/MDEPLOY-178_deployfile-with-embedded-pom/invoker.properties b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/invoker.properties
new file mode 100644
index 0000000..dd84d09
--- /dev/null
+++ b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/invoker.properties
@@ -0,0 +1,18 @@
+# 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.

+

+invoker.goals = package deploy:deploy-file
\ No newline at end of file
diff --git a/src/it/MDEPLOY-178_deployfile-with-embedded-pom/pom.xml b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/pom.xml
new file mode 100644
index 0000000..aa08993
--- /dev/null
+++ b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/pom.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>

+

+<!--

+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.

+-->

+

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

+  <modelVersion>4.0.0</modelVersion>

+

+  <groupId>org.apache.maven.plugins.deploy.its</groupId>

+  <artifactId>mdeploy178</artifactId>

+  <version>1.0</version>

+

+  <description>

+    Test to verify that the pom inside the jar in used when deploying the file

+  </description>

+

+  <url>https://jira.codehaus.org/browse/MDEPLOY-178</url>

+

+  <build>

+    <plugins>

+      <plugin>

+        <groupId>org.apache.maven.plugins</groupId>

+        <artifactId>maven-deploy-plugin</artifactId>

+        <version>@project.version@</version>

+      </plugin>

+    </plugins>

+  </build>

+

+</project>

diff --git a/src/it/MDEPLOY-178_deployfile-with-embedded-pom/test.properties b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/test.properties
new file mode 100644
index 0000000..992a650
--- /dev/null
+++ b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/test.properties
@@ -0,0 +1,19 @@
+# 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.

+

+file = target/mdeploy178-1.0.jar

+url = file:target/repo
\ No newline at end of file
diff --git a/src/it/MDEPLOY-178_deployfile-with-embedded-pom/verify.groovy b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/verify.groovy
new file mode 100644
index 0000000..2c6701a
--- /dev/null
+++ b/src/it/MDEPLOY-178_deployfile-with-embedded-pom/verify.groovy
@@ -0,0 +1,29 @@
+/*

+ * 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.

+ */

+

+assert new File( basedir, "target/repo/org/apache/maven/plugins/deploy/its/mdeploy178/1.0/mdeploy178-1.0.jar" ).exists()

+File deployedPom = new File( basedir, "target/repo/org/apache/maven/plugins/deploy/its/mdeploy178/1.0/mdeploy178-1.0.pom" )

+assert deployedPom.exists()

+

+File buildLog = new File( basedir, 'build.log' )

+assert buildLog.exists()

+assert buildLog.text.contains( "[DEBUG] Using META-INF/maven/org.apache.maven.plugins.deploy.its/mdeploy178/pom.xml as pomFile" )

+

+def pomProject = new XmlSlurper().parse( deployedPom )

+assert "https://jira.codehaus.org/browse/MDEPLOY-178".equals( pomProject.url.text() )
\ No newline at end of file
diff --git a/src/main/java/org/apache/maven/plugin/deploy/DeployFileMojo.java b/src/main/java/org/apache/maven/plugin/deploy/DeployFileMojo.java
index 5946832..db134b4 100644
--- a/src/main/java/org/apache/maven/plugin/deploy/DeployFileMojo.java
+++ b/src/main/java/org/apache/maven/plugin/deploy/DeployFileMojo.java
@@ -21,10 +21,17 @@
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Enumeration;
 import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Pattern;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
@@ -202,14 +209,86 @@
     void initProperties()
         throws MojoExecutionException
     {
-        // Process the supplied POM (if there is one)
         if ( pomFile != null )
         {
-            generatePom = false;
+            processModel( readModel( pomFile ) );
+        }
+        else
+        {
+            boolean foundPom = false;
 
-            Model model = readModel( pomFile );
+            JarFile jarFile = null;
+            try
+            {
+                Pattern pomEntry = Pattern.compile( "META-INF/maven/.*/pom\\.xml" );
 
-            processModel( model );
+                jarFile = new JarFile( file );
+
+                Enumeration<JarEntry> jarEntries = jarFile.entries();
+
+                while ( jarEntries.hasMoreElements() )
+                {
+                    JarEntry entry = jarEntries.nextElement();
+
+                    if ( pomEntry.matcher( entry.getName() ).matches() )
+                    {
+                        getLog().debug( "Using " + entry.getName() + " as pomFile" );
+
+                        foundPom = true;
+
+                        InputStream pomInputStream = null;
+                        OutputStream pomOutputStream = null;
+
+                        try
+                        {
+                            pomInputStream = jarFile.getInputStream( entry );
+                            
+                            String base = file.getName();
+                            if ( base.indexOf( '.' ) > 0 )
+                            {
+                                base = base.substring( 0, base.lastIndexOf( '.' ) );
+                            }
+                            pomFile = new File( file.getParentFile(), base + ".pom" );
+                            
+                            pomOutputStream = new FileOutputStream( pomFile );
+                            
+                            IOUtil.copy( pomInputStream, pomOutputStream );
+
+                            processModel( readModel( pomFile ) );
+
+                            break;
+                        }
+                        finally
+                        {
+                            IOUtil.close( pomInputStream );
+                            IOUtil.close( pomOutputStream );
+                        }
+                    }
+                }
+
+                if ( !foundPom )
+                {
+                    getLog().info( "pom.xml not found in " + file.getName() );
+                }
+            }
+            catch ( IOException e )
+            {
+                // ignore, artifact not packaged by Maven
+            }
+            finally
+            {
+                if ( jarFile != null )
+                {
+                    try
+                    {
+                        jarFile.close();
+                    }
+                    catch ( IOException e )
+                    {
+                        // we did our best
+                    }
+                }
+            }
         }
 
         if ( packaging == null && file != null )