Skip to content

Instantly share code, notes, and snippets.

@basilevs
Last active January 3, 2016 07:49
Show Gist options
  • Select an option

  • Save basilevs/8432073 to your computer and use it in GitHub Desktop.

Select an option

Save basilevs/8432073 to your computer and use it in GitHub Desktop.
Vozone Nsis Maven plugin trouble
package org.vozone.nsis;
/*
* Copyright 2001-2005 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.ArtifactRequest;
import org.sonatype.aether.resolution.ArtifactResolutionException;
import org.sonatype.aether.resolution.ArtifactResult;
import org.sonatype.aether.util.artifact.DefaultArtifact;
/**
* Goal which creates an NSIS installer from an RCP ZIP
*
* @goal create-installer
*
* @phase package
*/
public class VozoneNsisMojo extends AbstractMojo {
/**
* @parameter expression="${project.basedir}"
* @required
*/
private File projectDir;
/**
* @parameter expression="${project.build.directory}"
* @required
*/
private File outputDir;
/**
* @component
*/
private RepositorySystem repoSystem;
/**
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession repoSession;
/**
* @parameter default-value="${project.remoteProjectRepositories}"
* @readonly
*/
private List<RemoteRepository> remoteRepos;
/**
* Absolute location of makensis.exe
*
* @parameter expression="${org.vozone.nsis-location}"
* @required
* @readonly
*/
private String nsisExe;
/**
* Option to compress plugins with pack200. Significantly reduces installer
* size and increases build time and resource consumption.
*
* 384M+ of both heap and PermGen advised for Maven JVM if this option is
* enabled to avoid maven crashes.
*
* @parameter
* @required
* @readonly
*/
private boolean usePack200;
/**
* @parameter
* @required
*/
private String zipGAV;
/**
* NSIS Script, relative to the project root
*
* @parameter
* @required
*/
private String nsisScript;
public void execute() throws MojoExecutionException {
final File unzipDir = new File(outputDir, "product");
if (!unzipDir.exists()) {
if (!unzipDir.mkdirs()) {
throw new MojoExecutionException("Unable to create directory: " + unzipDir.getAbsolutePath());
}
}
final File nsisScriptFile = new File(projectDir, nsisScript);
if (!nsisScriptFile.exists()) {
throw new MojoExecutionException("NSIS script does not exist: "
+ nsisScriptFile.getAbsolutePath());
}
final ArtifactRequest request = new ArtifactRequest().setArtifact(new DefaultArtifact(zipGAV))
.setRepositories(remoteRepos);
final ArtifactResult zipArtifact;
try {
zipArtifact = repoSystem.resolveArtifact(repoSession, request);
} catch (final ArtifactResolutionException e) {
throw new MojoExecutionException("Unable to resolve ZIP (" + zipGAV + ")", e);
}
final String zipFile = zipArtifact.getArtifact().getFile().getAbsolutePath();
System.out.println("Extracting ZIP " + zipFile + " to " + unzipDir + " ...");
try {
final ZipFile file = new ZipFile(zipFile);
for (final Enumeration<? extends ZipEntry> e = file.entries(); e.hasMoreElements();) {
unzipEntry(file, e.nextElement(), unzipDir);
}
} catch (final Exception e) {
throw new MojoExecutionException("Unable to unzip " + zipFile, e);
}
System.out.println("Extracted");
if (usePack200) {
System.out.println("Packing with pack200");
Pack.main(new String[] { new File(unzipDir, "plugins").getAbsolutePath() });
}
final File installerFile = new File(outputDir, "VSD-Setup.exe");
final ProcessBuilder pb = new ProcessBuilder(nsisExe, nsisScriptFile.getAbsolutePath()).directory(
projectDir).redirectErrorStream(true);
pb.environment().put("NsisInput", unzipDir.getAbsolutePath());
pb.environment().put("NsisOutput", installerFile.getAbsolutePath());
final Process p;
try {
p = pb.start();
} catch (final Exception e) {
throw new MojoExecutionException("Unable to start NSIS process", e);
}
new Thread(new StreamReader(p.getInputStream())).start();
int exitCode;
try {
exitCode = p.waitFor();
} catch (final InterruptedException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
if (exitCode != 0) {
throw new MojoExecutionException("Non-zero makensis exit code: " + exitCode);
}
}
private void unzipEntry(final ZipFile zipfile, final ZipEntry entry, final File outputDir)
throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
final File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
final BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
final BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
copy(inputStream, outputStream);
} finally {
outputStream.close();
inputStream.close();
}
}
private final byte[] buffer = new byte[65535];
private void copy(final InputStream input, final OutputStream output) throws IOException {
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
}
private void createDir(final File dir) throws IOException {
if (!dir.mkdirs()) {
throw new IOException("Can not create directory " + dir);
}
}
private static class StreamReader implements Runnable {
private final BufferedReader reader;
public StreamReader(final InputStream in) {
this.reader = new BufferedReader(new InputStreamReader(in));
}
public void run() {
try {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("[NSIS] " + line);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.vozone.sdt</groupId>
<artifactId>org.vozone.sdt.installer</artifactId>
<version>7.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.vozone</groupId>
<artifactId>vozone-nsis-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<zipGAV>org.vozone.sdt:org.vozone.sdt.rcp:zip:win32.win32.x86:${project.version}</zipGAV>
<nsisScript>setup.nsi</nsisScript>
<usePack200>false</usePack200>
</configuration>
<executions>
<execution>
<id>create-installer</id>
<goals>
<goal>create-installer</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>env-xored</id>
<activation>
<property>
<name>build.env</name>
<value>xored</value>
</property>
</activation>
<distributionManagement>
<repository>
<id>xored</id>
<name>Xored Maven Repo</name>
<url>http://maven.xored.com/nexus/content/repositories/vz-releases/</url>
</repository>
<snapshotRepository>
<id>xored</id>
<name>Xored Maven Repo</name>
<url>http://maven.xored.com/nexus/content/repositories/vz-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
<id>xored</id>
<name>Xored snapshots</name>
<url>http://maven.xored.com/nexus/content/repositories/vz-snapshots/</url>
</pluginRepository>
<pluginRepository>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
<id>xored-releases</id>
<name>Xored releases</name>
<url>http://maven.xored.com/nexus/content/repositories/vz-releases/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<org.vozone.nsis-location>c:\program files\nsis\makensis.exe</org.vozone.nsis-location>
</properties>
</profile>
<profile>
<id>env-bt</id>
<activation>
<property>
<name>build.env</name>
<value>bt</value>
</property>
</activation>
<repositories>
<repository>
<id>bt-snapshots</id>
<url>https://collaborate.bt.com/artefacts/content/repositories/net-sdk-artifacts-snapshots/</url>
<name>BT snapshots</name>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>bt-releases</id>
<url>https://collaborate.bt.com/artefacts/content/repositories/net-sdk-artifacts-releases/</url>
<name>BT releases</name>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
<id>bt-releases</id>
<name>BT releases</name>
<url>https://collaborate.bt.com/artefacts/content/repositories/net-sdk-artifacts-releases/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<org.vozone.nsis-location>c:\program files\nsis\makensis.exe</org.vozone.nsis-location>
</properties>
</profile>
</profiles>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment