Commit 22bcb2ac authored by Adam Knapp's avatar Adam Knapp
Browse files

Improvements on the automated JAR building feature



Change-Id: I10e9985a47b11ae778a2bec921be04a006838538
Signed-off-by: default avatarAdam Knapp <adam.knapp@sigmatechnology.se>
parent 0e0ec75f
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.core;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.launching.AbstractVMInstall;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.titan.common.logging.ErrorReporter;
/**
* Provides backward compatibility for class path related code
* @author Adam Knapp
* */
public final class JavaRuntimeHelper {
/** @see org.eclipse.jdt.core.JavaCore#VERSION_1_8 */
public static final String VERSION_1_8 = "1.8";
/** @see org.eclipse.jdt.launching.IRuntimeClasspathEntry#MODULE_PATH */
public static final int MODULE_PATH = 4;
/** @see org.eclipse.jdt.launching.IRuntimeClasspathEntry#CLASS_PATH */
public static final int CLASS_PATH = 5;
/**
* Checks if configuration JRE is greater than 8.
* @param configuration the launch configuration
* @return boolean <code>true</code> if JRE used in configuration is greater than 8 else <code>false</code>
* @see org.eclipse.jdt.launching.JavaRuntime#isModularConfiguration
*/
public static boolean isModularConfiguration(ILaunchConfiguration configuration) {
try {
IVMInstall vm = JavaRuntime.computeVMInstall(configuration);
if (vm instanceof AbstractVMInstall) {
AbstractVMInstall install = (AbstractVMInstall) vm;
String vmver = install.getJavaVersion();
if (vmver == null) {
return false;
}
// versionToJdkLevel only handles 3 char versions = 1.5, 1.6, 1.7, etc
if (vmver.length() > 3) {
vmver = vmver.substring(0, 3);
}
if (VERSION_1_8.compareTo(vmver) < 0) {
return true;
}
}
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
}
return false;
}
}
......@@ -157,7 +157,7 @@ public final class AntLaunchConfigGenerator {
* Returns the singleton launch manager.
* @return launch manager
*/
public static ILaunchManager getLaunchManager() {
private static ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager();
}
......
......@@ -27,25 +27,25 @@ import javax.xml.transform.stream.StreamResult;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.jarpackagerfat.FatJarRsrcUrlBuilder;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.path.PathUtil;
import org.eclipse.titan.common.utils.CommentUtils;
import org.eclipse.titan.common.utils.FileUtils;
import org.eclipse.titan.common.utils.StringUtils;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.compiler.ProjectSourceCompiler;
import org.eclipse.titan.designer.core.JavaRuntimeHelper;
import org.eclipse.titan.designer.properties.data.MakefileCreationData;
import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
import org.w3c.dom.Document;
......@@ -107,14 +107,17 @@ public final class AntScriptGenerator {
/**
* Copies the {@code jar-in-jar-loader.zip} file into the project's {@code java_bin} folder
* @param project Project where {@code jar-in-jar-loader.zip} is required
* @throws CoreException
* @throws IOException
*/
private static void copyJarInJarLoader(IProject project) throws IOException {
final String pathString = GeneralConstants.JAVA_BUILD_DIR + File.separator + FatJarRsrcUrlBuilder.JAR_RSRC_LOADER_ZIP;
private static void copyJarInJarLoader(IProject project) throws CoreException, IOException {
final String pathString = GeneralConstants.JAVA_TEMP_DIR + File.separator + FatJarRsrcUrlBuilder.JAR_RSRC_LOADER_ZIP;
final IFile zipFile = project.getFile(new Path(pathString));
if (zipFile.exists()) {
return;
}
IFolder tempFolder = project.getFolder(new Path(GeneralConstants.JAVA_TEMP_DIR));
FileUtils.createDir(tempFolder);
final URI zipURI = URIUtil.toURI(zipFile.getLocation());
if (zipURI == null) {
throw new IOException("Path error: " + pathString);
......@@ -144,25 +147,6 @@ public final class AntScriptGenerator {
return buildFile.exists();
}
/**
* Searches for a launch configuration of Java application type that is related to the specified project.
* The launch configuration is used to get the class paths.
* @param project Project where the launch configuration is looked for
* @return The launch configuration of Java application type
* @throws CoreException
*/
private static ILaunchConfiguration findLaunchConfiguration(final IProject project) throws CoreException {
ILaunchConfigurationType type = DebugPlugin.getDefault().getLaunchManager().
getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(type);
for (ILaunchConfiguration config : configs) {
if (project.getName().equals(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""))) {
return config;
}
}
return null;
}
/**
* Creates and then stores the {@code jarbuild.xml} ANT script for the specified project's root
* @param project Project where the ANT script is required
......@@ -216,7 +200,7 @@ public final class AntScriptGenerator {
ErrorReporter.INTERNAL_ERROR("Jar file is null or empty");
return null;
}
final ILaunchConfiguration config = findLaunchConfiguration(project);
final ILaunchConfiguration config = JavaAppLaunchConfigGenerator.findLaunchConfiguration(project);
if (config == null) {
ErrorReporter.parallelErrorDisplayInMessageDialog(
"Error while generating the ANT script 'jarbuild.xml' for project",
......@@ -225,6 +209,7 @@ public final class AntScriptGenerator {
return null;
}
final SourceInfo[] sourceInfos = convert(getClasspath(config));
JavaAppLaunchConfigGenerator.deleteTemporaryJavaAppLaunchConfiguration(project);
DocumentBuilder docBuilder = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
......@@ -325,19 +310,21 @@ public final class AntScriptGenerator {
manifest.appendChild(attribute);
Element zipfileset = document.createElement("zipfileset");
zipfileset.setAttribute("src", "${dir.build}/" + FatJarRsrcUrlBuilder.JAR_RSRC_LOADER_ZIP);
zipfileset.setAttribute("src", GeneralConstants.JAVA_TEMP_DIR + "/" + FatJarRsrcUrlBuilder.JAR_RSRC_LOADER_ZIP);
jar.appendChild(zipfileset);
for (SourceInfo sourceInfo : sourceInfos) {
if (sourceInfo.isJar) {
final File sourceJarFile = new File(sourceInfo.absPath);
final String relPath = PathUtil.getRelativePath(project.getLocation().toOSString(), sourceJarFile.getParent()).replace("\\", "/");
Element fileset = document.createElement("zipfileset");
fileset.setAttribute("dir", PathUtil.getRelativePath(project.getLocation().toOSString(), sourceJarFile.getParent()));
fileset.setAttribute("dir", relPath);
fileset.setAttribute("includes", sourceJarFile.getName());
jar.appendChild(fileset);
} else {
Element fileset = document.createElement("fileset");
fileset.setAttribute("dir", PathUtil.getRelativePath(project.getLocation().toOSString(), sourceInfo.absPath));
final String relPath = PathUtil.getRelativePath(project.getLocation().toOSString(), sourceInfo.absPath).replace("\\", "/");
fileset.setAttribute("dir", relPath);
jar.appendChild(fileset);
}
}
......@@ -357,11 +344,11 @@ public final class AntScriptGenerator {
entries = JavaRuntime.resolveRuntimeClasspath(entries, configuration);
final ArrayList<IPath> userEntries = new ArrayList<IPath>(entries.length);
final boolean isModularConfig = JavaRuntime.isModularConfiguration(configuration);
final boolean isModularConfig = JavaRuntimeHelper.isModularConfiguration(configuration);
for (final IRuntimeClasspathEntry cpentry : entries) {
final int classPathProperty= cpentry.getClasspathProperty();
if ((!isModularConfig && classPathProperty == IRuntimeClasspathEntry.USER_CLASSES)
|| (isModularConfig && (classPathProperty == IRuntimeClasspathEntry.CLASS_PATH || classPathProperty == IRuntimeClasspathEntry.MODULE_PATH))) {
|| (isModularConfig && (classPathProperty == JavaRuntimeHelper.CLASS_PATH || classPathProperty == JavaRuntimeHelper.MODULE_PATH))) {
final String location = cpentry.getLocation();
if (location != null) {
final IPath entry = Path.fromOSString(location);
......
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.core.ant;
import java.io.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.titan.common.utils.FileUtils;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.compiler.ProjectSourceCompiler;
/**
* Utility class for generating temporary Java app launch configuration.
* @author Adam Knapp
*/
public final class JavaAppLaunchConfigGenerator {
private static final String TEMPORARY_LAUNCH_CONFIGURATION_NAME = "tempJavaAppLaunchConfig";
/**
* Creates a temporary Java app launch configuration.
* @param project Project to which the temporary Java app launch configuration is related to
* @return The temporary Java app launch configuration. It returns {@code null}, if the specified project is {@code null}.
* @throws CoreException
*/
public static ILaunchConfiguration createTemporaryJavaAppLaunchConfiguration(final IProject project) throws CoreException {
if (project == null) {
return null;
}
ILaunchConfiguration tempConfig = null;
ILaunchConfigurationWorkingCopy wc = null;
final ILaunchConfigurationType type = getJavaAppLaunchConfigurationType();
final ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(type);
for (ILaunchConfiguration config : configs) {
if (config.getName().equals(TEMPORARY_LAUNCH_CONFIGURATION_NAME)) {
tempConfig = config;
break;
}
}
if (tempConfig == null) {
IFolder tempFolder = project.getFolder(new Path(GeneralConstants.JAVA_TEMP_DIR));
FileUtils.createDir(tempFolder);
wc = getJavaAppLaunchConfigurationType().newInstance(tempFolder,
getLaunchManager().generateLaunchConfigurationName(TEMPORARY_LAUNCH_CONFIGURATION_NAME));
} else {
wc = tempConfig.getWorkingCopy();
}
final String mainType = ProjectSourceCompiler.getPackageGeneratedRoot(project) + ".Parallel_main";
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainType);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getName());
final String mainFilePath = ProjectSourceCompiler.getGeneratedRoot(project) + File.separator + "Parallel_main";
final IFile mainFile = project.getFile(new Path(mainFilePath));
wc.setMappedResources(new IResource[] {mainFile});
tempConfig = wc.doSave();
return tempConfig;
}
/**
* Deletes the temporary Java app launch configuration.
* @param project Project to which the temporary Java app launch configuration is related to
* @throws CoreException
*/
public static void deleteTemporaryJavaAppLaunchConfiguration(final IProject project) throws CoreException {
if (project == null) {
return;
}
IFile tempConfigFile = project.getFile(new Path(GeneralConstants.JAVA_TEMP_DIR + File.separator
+ TEMPORARY_LAUNCH_CONFIGURATION_NAME + ".launch"));
if (tempConfigFile.exists()) {
tempConfigFile.delete(true, null);
}
}
/**
* Searches for a launch configuration of Java application type that is related to the specified project.
* The launch configuration is used to get the class paths.
* @param project Project where the launch configuration is looked for
* @return The launch configuration of Java application type
* @throws CoreException
*/
public static ILaunchConfiguration findLaunchConfiguration(final IProject project) throws CoreException {
if (project == null) {
return null;
}
final ILaunchConfigurationType type = getJavaAppLaunchConfigurationType();
final ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(type);
for (ILaunchConfiguration config : configs) {
if (project.getName().equals(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""))) {
return config;
}
}
return createTemporaryJavaAppLaunchConfiguration(project);
}
/**
* Returns the launch configuration type extension for Java app launch configurations.
* @return The launch configuration type extension for Java app launch configurations
* @see org.eclipse.debug.core.ILaunchManager#getLaunchConfigurationType
*/
public static ILaunchConfigurationType getJavaAppLaunchConfigurationType() {
return getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
}
/**
* Returns the singleton launch manager.
* @return launch manager
*/
private static ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment