mirror of https://github.com/apache/maven.git
Fix jar creation with code present in j2ee mojo.
It's a temporary change before use plexus-archiver. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@162858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4abf102820
commit
07cfd28f67
|
@ -14,6 +14,11 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<package>org.apache.maven.plugin.jar</package>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>maven</groupId>
|
||||
<artifactId>maven-artifact</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>maven</groupId>
|
||||
<artifactId>maven-core</artifactId>
|
||||
|
|
|
@ -0,0 +1,233 @@
|
|||
package org.apache.maven.plugin.jar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Copyright 2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import org.apache.maven.artifact.MavenArtifact;
|
||||
import org.apache.maven.plugin.AbstractPlugin;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.codehaus.plexus.util.DirectoryScanner;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Base class for tasks that build archives in JAR file format.
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public abstract class AbstractJarMojo
|
||||
extends AbstractPlugin
|
||||
{
|
||||
private byte[] buffer = new byte[4096];
|
||||
|
||||
/**
|
||||
* Add artifacts from tagged dependencies to the archive.
|
||||
* @param includes a map <String, File> of items to be include in the outpur
|
||||
* @param project the project object model
|
||||
* @param tag the property tag to look for; for example "jar.bundle"
|
||||
* @param pathTag the property tag that specifies the target path; for example, jar.target.path
|
||||
*/
|
||||
protected void addTaggedDependencies(Map includes, MavenProject project, String tag, String pathTag) {
|
||||
addTaggedDependencies(includes, "", project, tag, pathTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add artifacts from tagged dependencies to the archive. For example, the definition:
|
||||
* <code>
|
||||
* <dependency>
|
||||
* <artifactId>my-library</artifactId>
|
||||
* <version>1.0.1</version>
|
||||
* <property>
|
||||
* <jar>my-library.jar
|
||||
* <property>
|
||||
* </dependency>
|
||||
* </code>
|
||||
* would result in the archive <code>my-library-1.0.1.jar</code> being included
|
||||
* in the output jar as /my-library.jar. The entry name will default to the base
|
||||
* name of the archive in the root: <code>/my-library-1.0.1.jar</code>
|
||||
*
|
||||
* @param includes a map <String, File> of items to be include in the outpur
|
||||
* @param prefix to be added to the jar entry name of each item included
|
||||
* @param project the project object model
|
||||
* @param tag the property tag to look for; for example "jar.bundle"
|
||||
* @param pathTag the property tag that specifies the target path; for example, jar.target.path
|
||||
*/
|
||||
protected void addTaggedDependencies(Map includes, String prefix, MavenProject project, String tag, String pathTag) {
|
||||
for (Iterator i = project.getArtifacts().iterator(); i.hasNext();) {
|
||||
MavenArtifact artifact = (MavenArtifact) i.next();
|
||||
Properties properties = artifact.getDependency().getProperties();
|
||||
if (Boolean.valueOf(properties.getProperty(tag)).booleanValue()) {
|
||||
File file = new File(artifact.getPath());
|
||||
String targetPath = properties.getProperty(pathTag, file.getName());
|
||||
includes.put(prefix + targetPath, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all files in the specified directory to the archive.
|
||||
*
|
||||
* @param includes a map <String, File> of items to be include in the outpur
|
||||
* @param baseDir the directory to add
|
||||
*/
|
||||
protected void addDirectory(Map includes, File baseDir) throws IOException {
|
||||
addDirectory(includes, "", baseDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all files in the specified directory to the archive.
|
||||
*
|
||||
* @param includes a map <String, File> of items to be include in the outpur
|
||||
* @param prefix value to be added to the front of jar entry names
|
||||
* @param baseDir the directory to add
|
||||
*/
|
||||
protected void addDirectory(Map includes, String prefix, File baseDir) throws IOException {
|
||||
addDirectory( includes, null, null, prefix, baseDir );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all files in the specified directory to the archive.
|
||||
*
|
||||
* @param includes a map <String, File> of items to be include in the outpur
|
||||
* @param includesPattern Sets the list of include patterns to use
|
||||
* @param excludesPattern Sets the list of exclude patterns to use
|
||||
* @param prefix value to be added to the front of jar entry names
|
||||
* @param baseDir the directory to add
|
||||
*/
|
||||
protected void addDirectory(Map includes, String includesPattern, String excludesPattern, String prefix, File baseDir) throws IOException {
|
||||
DirectoryScanner scanner = new DirectoryScanner();
|
||||
scanner.setBasedir(baseDir);
|
||||
if ( includesPattern != null )
|
||||
{
|
||||
scanner.setIncludes( StringUtils.split( includesPattern, "," ) );
|
||||
}
|
||||
|
||||
if ( excludesPattern != null )
|
||||
{
|
||||
scanner.setExcludes( StringUtils.split( excludesPattern, "," ) );
|
||||
}
|
||||
scanner.scan();
|
||||
String[] files = scanner.getIncludedFiles();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
String file = files[i];
|
||||
file = file.replace('\\', '/'); // todo shouldn't the scanner return platform independent names?
|
||||
includes.put(prefix + file, new File(baseDir, file));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the jar file specified and include the listed files.
|
||||
*
|
||||
* @param jarFile the jar file to create
|
||||
* @param includes a Map<String, File>of items to include; the key is the jar entry name
|
||||
* @throws IOException if there is a problem writing the archive or reading the sources
|
||||
*/
|
||||
protected void createJar(File jarFile, Map includes) throws IOException {
|
||||
JarOutputStream jos = createJar(jarFile, createManifest());
|
||||
try {
|
||||
addEntries(jos, includes);
|
||||
} finally {
|
||||
jos.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a manifest for the jar file
|
||||
*
|
||||
* @return a default manifest; the Manifest-Version and Created-By attributes are initialized
|
||||
*/
|
||||
protected Manifest createManifest() {
|
||||
Manifest mf = new Manifest();
|
||||
Attributes attrs = mf.getMainAttributes();
|
||||
attrs.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
|
||||
attrs.putValue("Created-By", "2.0 (Apache Maven)");
|
||||
return mf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the specified jar file and return a JarOutputStream to it
|
||||
*
|
||||
* @param jarFile the jar file to create
|
||||
* @param mf the manifest to use
|
||||
* @return a JarOutputStream that can be used to write to that file
|
||||
* @throws IOException if there was a problem opening the file
|
||||
*/
|
||||
protected JarOutputStream createJar(File jarFile, Manifest mf) throws IOException {
|
||||
jarFile.getParentFile().mkdirs();
|
||||
FileOutputStream fos = new FileOutputStream(jarFile);
|
||||
try {
|
||||
return new JarOutputStream(fos, mf);
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
fos.close();
|
||||
jarFile.delete();
|
||||
} catch (IOException e1) {
|
||||
// ignore
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all entries in the supplied Map to the jar
|
||||
*
|
||||
* @param jos a JarOutputStream that can be used to write to the jar
|
||||
* @param includes a Map<String, File> of entries to add
|
||||
* @throws IOException if there is a problem writing the archive or reading the sources
|
||||
*/
|
||||
protected void addEntries(JarOutputStream jos, Map includes) throws IOException {
|
||||
for (Iterator i = includes.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
String name = (String) entry.getKey();
|
||||
File file = (File) entry.getValue();
|
||||
addEntry(jos, name, file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single entry to the jar
|
||||
*
|
||||
* @param jos a JarOutputStream that can be used to write to the jar
|
||||
* @param name the entry name to use; must be '/' delimited
|
||||
* @param source the file to add
|
||||
* @throws IOException if there is a problem writing the archive or reading the sources
|
||||
*/
|
||||
protected void addEntry(JarOutputStream jos, String name, File source) throws IOException {
|
||||
FileInputStream fis = new FileInputStream(source);
|
||||
try {
|
||||
jos.putNextEntry(new JarEntry(name));
|
||||
int count;
|
||||
while ((count = fis.read(buffer)) > 0) {
|
||||
jos.write(buffer, 0, count);
|
||||
}
|
||||
jos.closeEntry();
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,21 +16,12 @@ package org.apache.maven.plugin.jar;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import org.apache.maven.plugin.AbstractPlugin;
|
||||
import org.apache.maven.plugin.PluginExecutionRequest;
|
||||
import org.apache.maven.plugin.PluginExecutionResponse;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @goal jar
|
||||
|
@ -66,7 +57,7 @@ import java.util.jar.Manifest;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class JarMojo
|
||||
extends AbstractPlugin
|
||||
extends AbstractJarMojo
|
||||
{
|
||||
public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
|
||||
throws Exception
|
||||
|
@ -88,92 +79,10 @@ public class JarMojo
|
|||
|
||||
File jarFile = new File( new File( outputDirectory ), jarName + ".jar" );
|
||||
|
||||
List files = FileUtils.getFileNames( basedir, "**/**", "**/package.html", false );
|
||||
|
||||
createJar( files, jarFile, basedir );
|
||||
}
|
||||
|
||||
public void createJar( List files, File jarName, File basedir )
|
||||
throws Exception
|
||||
{
|
||||
JarOutputStream jar = new JarOutputStream( new FileOutputStream( jarName ), createManifest() );
|
||||
|
||||
try
|
||||
{
|
||||
for ( int i = 0; i < files.size(); i++ )
|
||||
{
|
||||
String file = (String) files.get( i );
|
||||
|
||||
writeJarEntry( jar, new File( basedir, file ), file );
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
jar.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void writeJarEntry( JarOutputStream jar, File source, String entryName )
|
||||
throws Exception
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int bytesRead;
|
||||
|
||||
try
|
||||
{
|
||||
FileInputStream is = new FileInputStream( source );
|
||||
|
||||
try
|
||||
{
|
||||
JarEntry entry = new JarEntry( entryName );
|
||||
|
||||
jar.putNextEntry( entry );
|
||||
|
||||
while ( ( bytesRead = is.read( buffer ) ) != -1 )
|
||||
{
|
||||
jar.write( buffer, 0, bytesRead );
|
||||
}
|
||||
}
|
||||
catch ( Exception ex )
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
catch ( IOException ex )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private Manifest createManifest()
|
||||
{
|
||||
Manifest manifest = null;
|
||||
Map includes = new LinkedHashMap();
|
||||
|
||||
try
|
||||
{
|
||||
// Construct a string version of a manifest
|
||||
StringBuffer sbuf = new StringBuffer();
|
||||
|
||||
sbuf.append("Manifest-Version: 1.0\n");
|
||||
|
||||
sbuf.append("Created-By: Apache Maven\n");
|
||||
|
||||
sbuf.append("Built-By: " + System.getProperty("user.name") + "\n");
|
||||
|
||||
// Convert the string to a input stream
|
||||
InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
|
||||
|
||||
// Create the manifest
|
||||
manifest = new Manifest(is);
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
manifest = new Manifest();
|
||||
}
|
||||
|
||||
return manifest;
|
||||
addDirectory(includes, "**/**", "**/package.html", "", basedir);
|
||||
|
||||
createJar( jarFile, includes );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue