if ( serverXml != null && serverXml.exists() )
{
os.putArchiveEntry( new JarArchiveEntry( "conf/server.xml" ) );
IOUtils.copy( new FileInputStream( serverXml ), os );
os.closeArchiveEntry();
properties.put( Tomcat7Runner.USE_SERVER_XML_KEY, Boolean.TRUE.toString() );
}
else
{
properties.put( Tomcat7Runner.USE_SERVER_XML_KEY, Boolean.FALSE.toString() );
}
os.putArchiveEntry( new JarArchiveEntry( "conf/web.xml" ) );
IOUtils.copy( getClass().getResourceAsStream( "/conf/web.xml" ), os );
os.closeArchiveEntry();
properties.store( tmpPropertiesFileOutputStream, "created by Apache Tomcat Maven plugin" );
tmpPropertiesFileOutputStream.flush();
tmpPropertiesFileOutputStream.close();
os.putArchiveEntry( new JarArchiveEntry( Tomcat7RunnerCli.STAND_ALONE_PROPERTIES_FILENAME ) );
IOUtils.copy( new FileInputStream( tmpPropertiesFile ), os );
os.closeArchiveEntry();
// add tomcat classes
for ( Artifact pluginArtifact : pluginArtifacts )
{
if ( StringUtils.equals( "org.apache.tomcat", pluginArtifact.getGroupId() ) || StringUtils.equals(
"org.apache.tomcat.embed", pluginArtifact.getGroupId() ) || StringUtils.equals(
"org.eclipse.jdt.core.compiler", pluginArtifact.getGroupId() ) || StringUtils.equals( "commons-cli",
pluginArtifact.getArtifactId() )
|| StringUtils.equals( "tomcat7-war-runner", pluginArtifact.getArtifactId() ) )
{
JarFile jarFile = new JarFile( pluginArtifact.getFile() );
extractJarToArchive( jarFile, os );
}
}
// add extra dependencies
if ( extraDependencies != null && !extraDependencies.isEmpty() )
{
for ( Dependency dependency : extraDependencies )
{
// String groupId, String artifactId, String version, String scope, String type
Artifact artifact =
artifactFactory.createArtifact( dependency.getGroupId(), dependency.getArtifactId(),
dependency.getVersion(), dependency.getScope(),
dependency.getType() );
artifactResolver.resolve( artifact, this.remoteRepos, this.local );
JarFile jarFile = new JarFile( artifact.getFile() );
extractJarToArchive( jarFile, os );
}
}
Manifest manifest = new Manifest();
Manifest.Attribute mainClassAtt = new Manifest.Attribute();
mainClassAtt.setName( "Main-Class" );
mainClassAtt.setValue( mainClass );
manifest.addConfiguredAttribute( mainClassAtt );
manifest.write( tmpManifestWriter );
tmpManifestWriter.flush();
tmpManifestWriter.close();
os.putArchiveEntry( new JarArchiveEntry( "META-INF/MANIFEST.MF" ) );
IOUtils.copy( new FileInputStream( tmpManifestFile ), os );
os.closeArchiveEntry();
if ( attachArtifact )
{
//MavenProject project, String artifactType, String artifactClassifier, File artifactFile
projectHelper.attachArtifact( project, attachArtifactClassifierType, attachArtifactClassifier,
execWarJar );
}
if ( extraResources != null )
{
for ( ExtraResource extraResource : extraResources )
{
DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setBasedir( extraResource.getDirectory() );
directoryScanner.addDefaultExcludes();
directoryScanner.setExcludes( toStringArray( extraResource.getExcludes() ) );
directoryScanner.setIncludes( toStringArray( extraResource.getIncludes() ) );
directoryScanner.scan();
for ( String includeFile : directoryScanner.getIncludedFiles() )
{
getLog().debug( "include file:" + includeFile );
os.putArchiveEntry( new JarArchiveEntry( includeFile ) );
IOUtils.copy( new FileInputStream( new File( extraResource.getDirectory(), includeFile ) ),
os );
os.closeArchiveEntry();
}
}
}
if ( tomcatConfigurationFilesDirectory != null && tomcatConfigurationFilesDirectory.exists() )
{
// Because its the tomcat default dir for configs
String aConfigOutputDir = "conf/";
copyDirectoryContentIntoArchive( tomcatConfigurationFilesDirectory, aConfigOutputDir, os );
}
}
catch ( ManifestException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( ArchiveException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( ArtifactNotFoundException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( ArtifactResolutionException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
finally
{
IOUtils.closeQuietly( os );
IOUtils.closeQuietly( tmpManifestWriter );
IOUtils.closeQuietly( execWarJarOutputStream );
IOUtils.closeQuietly( tmpPropertiesFileOutputStream );
}
}