diff options
author | Gautam Shah <gautams@amdocs.com> | 2018-06-01 08:38:32 +0530 |
---|---|---|
committer | GAUTAMS <gautams@amdocs.com> | 2018-06-04 13:53:51 +0530 |
commit | 03205dad582ffa5db54187f451b9a29d3d0af386 (patch) | |
tree | 21e67f2a40764fe1b1904e58cd775c0041528352 /openecomp-be/tools/compile-helper-plugin/src/main/java/org | |
parent | 06b1f35479a40194c6d84c38cc1215ae76b81ba6 (diff) |
Fixing Merge Build issue and Sonar issues.
fixing build issue and Sonar issues.
Change-Id: I4209b79ab2a3646839df50be4b58f8f230b6d710
Issue-ID: SDC-1189
Signed-off-by: GAUTAMS <gautams@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/compile-helper-plugin/src/main/java/org')
5 files changed, 151 insertions, 33 deletions
diff --git a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java index 681e5f71bd..1b1278d797 100644 --- a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java +++ b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java @@ -16,9 +16,13 @@ package org.openecomp.sdc.onboarding; +import static org.openecomp.sdc.onboarding.Constants.CHECKSUM; +import static org.openecomp.sdc.onboarding.Constants.COLON; +import static org.openecomp.sdc.onboarding.Constants.DOT; import static org.openecomp.sdc.onboarding.Constants.JAVA_EXT; import static org.openecomp.sdc.onboarding.Constants.UNICORN; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -40,16 +44,44 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; import java.util.stream.Collectors; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; class BuildHelper { + private static Log logger; + + private static Map<String, String> store = new HashMap<>(); + private BuildHelper() { // donot remove. } + static void setLogger(Log log) { + logger = log; + } + + static String getSnapshotSignature(File snapshotFile, String moduleCoordinate, String version) { + String key = moduleCoordinate + ":" + version; + String signature = store.get(key); + if (signature != null) { + return signature; + } + try { + signature = new String(fetchSnapshotSignature(snapshotFile, version)); + store.put(key, signature); + return signature; + } catch (IOException ioe) { + logger.debug(ioe); + return version; + } + + } + static long getChecksum(File file, String fileType) { try { return readSources(file, fileType).hashCode(); @@ -76,8 +108,7 @@ class BuildHelper { if (file.exists()) { List<File> list = Files.walk(Paths.get(file.getAbsolutePath())) .filter(JAVA_EXT.equals(fileType) ? BuildHelper::isRegularJavaFile : - Files::isRegularFile).map(p -> p.toFile()) - .collect(Collectors.toList()); + Files::isRegularFile).map(Path::toFile).collect(Collectors.toList()); source.putAll(ForkJoinPool.commonPool() .invoke(new FileReadTask(list.toArray(new File[0]), file.getAbsolutePath()))); } @@ -143,7 +174,8 @@ class BuildHelper { static Optional<String> getArtifactPathInLocalRepo(String repoPath, MavenProject project, byte[] sourceChecksum) throws MojoFailureException { - + store.put(project.getGroupId() + COLON + project.getArtifactId() + COLON + project.getVersion(), + new String(sourceChecksum)); URI uri = null; try { uri = new URI(repoPath + (project.getGroupId().replace('.', '/')) + '/' + project.getArtifactId() + '/' @@ -155,9 +187,8 @@ class BuildHelper { File[] list = f.listFiles(t -> t.getName().equals(project.getArtifactId() + "-" + project.getVersion() + "." + project.getPackaging())); if (list != null && list.length > 0) { - File checksumFile = new File(list[0].getParentFile(), project.getBuild().getFinalName() + "." + UNICORN); try { - if (checksumFile.exists() && Arrays.equals(sourceChecksum, Files.readAllBytes(checksumFile.toPath()))) { + if (Arrays.equals(sourceChecksum, fetchSnapshotSignature(list[0], project.getVersion()))) { return Optional.of(list[0].getAbsolutePath()); } } catch (IOException e) { @@ -167,12 +198,27 @@ class BuildHelper { return Optional.empty(); } + private static byte[] fetchSnapshotSignature(File file, String version) throws IOException { + byte[] data = Files.readAllBytes(file.toPath()); + try (ByteArrayInputStream bais = new ByteArrayInputStream(data); + JarInputStream jis = new JarInputStream(bais)) { + JarEntry entry = null; + while ((entry = jis.getNextJarEntry()) != null) { + if (entry.getName().equals(UNICORN + DOT + CHECKSUM)) { + byte[] sigStore = new byte[1024]; + return new String(sigStore, 0, jis.read(sigStore, 0, 1024)).getBytes(); + } + } + } + return version.getBytes(); + } + static <T> Optional<T> readState(String fileName, Class<T> clazz) { try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); ObjectInputStream ois = new ObjectInputStream(is)) { return Optional.of(clazz.cast(ois.readObject())); } catch (Exception ignored) { - //ignore. it is taken care. + logger.debug(ignored); return Optional.empty(); } } diff --git a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java index ead6e7b755..1ce60a636c 100644 --- a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java +++ b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java @@ -28,9 +28,13 @@ import static org.openecomp.sdc.onboarding.Constants.RESOURCE_BUILD_DATA; import static org.openecomp.sdc.onboarding.Constants.SKIP_MAIN_SOURCE_COMPILE; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.nio.file.Paths; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -39,36 +43,59 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; public class BuildState { + private static final String SHUTDOWN_TIME = "shutdownTime"; + private static Map<String, Map> compileDataStore = new HashMap<>(); private static Map<String, Object> moduleBuildData = new HashMap<>(); private static Map<String, Object> resourceBuildData = new HashMap<>(); private static Map<String, Artifact> artifacts = new HashMap<>(); private static Set<String> executeTestsIfDependsOnStore = new HashSet<>(); private static Set<String> pmdExecutedInRun = new HashSet<>(); + private static File stateFileLocation = + new File(Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile(), "compileState.dat"); private static File compileStateFile; private MavenProject project; private String compileStateFilePath; + private static Log logger; static { initializeStore(); Optional<HashMap> masterStore = readState("compile.dat", HashMap.class); compileDataStore = masterStore.isPresent() ? masterStore.get() : compileDataStore; + if (stateFileLocation.exists()) { + HashMap dat = loadState(stateFileLocation); + if (swapStates((HashMap<?, ?>) compileDataStore, dat)) { + compileDataStore = dat; + } + } + } + + private static void setLogger(Log log) { + logger = log; } - void init() { + void init(Log log) { + setLogger(log); artifacts.clear(); for (Artifact artifact : project.getArtifacts()) { if (artifact.isSnapshot() && JAR.equals(artifact.getType())) { artifacts.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact); } } - compileStateFile = - getCompileStateFile(compileStateFilePath.substring(0, compileStateFilePath.indexOf('/')), project); + if (compileStateFile == null) { + setCompileStateFile( + getCompileStateFile(compileStateFilePath.substring(0, compileStateFilePath.indexOf('/')), project)); + } + } + + private static void setCompileStateFile(File file) { + compileStateFile = file; } static void initializeStore() { @@ -124,12 +151,13 @@ public class BuildState { Long lastTime = Long.class.cast(compileDataStore.get(FULL_BUILD_DATA).put(moduleCoordinates, buildTime)); try { if (lastTime == null || !lastTime.equals(buildTime)) { - if (!project.getProperties().containsKey(SKIP_MAIN_SOURCE_COMPILE)) { + boolean skipMainCompile = project.getProperties().containsKey(SKIP_MAIN_SOURCE_COMPILE); + if (!skipMainCompile) { writeCompileState(); } } } catch (IOException ignored) { - // ignored. No need to handle. System will take care. + logger.debug(ignored); } } @@ -140,7 +168,7 @@ public class BuildState { compileDataStore.get(FULL_RESOURCE_BUILD_DATA).put(moduleCoordinates, buildTime); writeCompileState(); } catch (IOException ignored) { - // ignored. No need to handle. System will take care. + logger.debug(ignored); } } } @@ -182,7 +210,7 @@ public class BuildState { ObjectOutputStream ois = new ObjectOutputStream(fos)) { ois.writeObject(dataToSave); } catch (IOException ignored) { - //ignored. do nothing. system will take care. + logger.debug(ignored); } } } @@ -209,10 +237,11 @@ public class BuildState { boolean isCompileMust(String moduleCoordinates, Collection<String> dependencies) { for (String d : dependencies) { if (artifacts.containsKey(d) && JAR.equals(artifacts.get(d).getType())) { - if (artifacts.get(d).getVersion().equals(project.getVersion()) && getBuildTime(d) == 0) { - System.out.println(ANSI_YELLOW + "[WARNING:]" + "You have module[" + d - + "] not locally compiled even once, please compile your project once daily from root to have reliable build results." - + ANSI_COLOR_RESET); + boolean versionEqual = artifacts.get(d).getVersion().equals(project.getVersion()); + if (versionEqual && getBuildTime(d) == 0) { + logger.warn(ANSI_YELLOW + "[WARNING:]" + "You have module[" + d + + "] not locally compiled even once, please compile your project once daily from root to have reliable build results." + + ANSI_COLOR_RESET); return true; } } @@ -248,4 +277,25 @@ public class BuildState { return false; } + private static HashMap loadState(File file) { + try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) { + return HashMap.class.cast(ois.readObject()); + } catch (Exception e) { + logger.debug(e); + return new HashMap<>(); + } + } + + private static boolean swapStates(HashMap repo, HashMap last) { + Long repoTime = repo.get(SHUTDOWN_TIME) == null ? 0 : (Long) repo.get(SHUTDOWN_TIME); + Long lastTime = last.get(SHUTDOWN_TIME) == null ? 0 : (Long) last.get(SHUTDOWN_TIME); + long repoBuildNumber = repoTime / 1000; + long lastBuildNumber = lastTime / 1000; + if (repoBuildNumber != lastBuildNumber) { + return false; + } + return Long.compare(repoTime, lastTime) < 0; + } + + } diff --git a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/Constants.java b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/Constants.java index fb69cce3cb..195b356377 100644 --- a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/Constants.java +++ b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/Constants.java @@ -22,7 +22,6 @@ public class Constants { public static final String UNICORN = "unicorn"; public static final String EMPTY_STRING = ""; public static final String PREFIX = System.getProperties().contains(UNICORN) ? EMPTY_STRING : UNICORN; - ; public static final String JACOCO_SKIP = "jacoco.skip"; public static final String FORK_COUNT = "fork.count"; public static final String FORK_MODE = "fork.mode"; @@ -39,8 +38,10 @@ public class Constants { public static final String SKIP_MAIN_SOURCE_COMPILE = PREFIX + "skipMainSourceCompile"; public static final String SKIP_TEST_SOURCE_COMPILE = PREFIX + "skipTestSourceCompile"; public static final String MAIN_CHECKSUM = "mainChecksum"; + public static final String CHECKSUM = "checksum"; public static final String TEST_CHECKSUM = "testChecksum"; public static final String RESOURCE_CHECKSUM = "resourceChecksum"; + public static final String TEST_RESOURCE_CHECKSUM = "testResourceChecksum"; public static final String MAIN_SOURCE_CHECKSUM = "mainSourceChecksum"; public static final String TEST_SOURCE_CHECKSUM = "testSourceChecksum"; public static final String GENERATED_SOURCE_CHECKSUM = "generatedSourceChecksum"; @@ -65,4 +66,6 @@ public class Constants { private Constants() { } + + } diff --git a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/InitializationHelperMojo.java b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/InitializationHelperMojo.java index 9aa48b2174..42ddc657db 100644 --- a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/InitializationHelperMojo.java +++ b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/InitializationHelperMojo.java @@ -64,12 +64,15 @@ public class InitializationHelperMojo extends AbstractMojo { project.getProperties().setProperty(SKIP_PMD, Boolean.TRUE.toString()); if (System.getProperties().containsKey(UNICORN)) { - buildState.init(); + buildState.init(getLog()); + BuildHelper.setLogger(getLog()); } else { project.getProperties().setProperty("skipMainSourceCompile", Boolean.FALSE.toString()); project.getProperties().setProperty("skipTestSourceCompile", Boolean.FALSE.toString()); } + } + } diff --git a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java index 59242715d2..e97b172b84 100644 --- a/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java +++ b/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java @@ -18,9 +18,11 @@ package org.openecomp.sdc.onboarding; import static org.openecomp.sdc.onboarding.BuildHelper.getArtifactPathInLocalRepo; import static org.openecomp.sdc.onboarding.BuildHelper.getChecksum; +import static org.openecomp.sdc.onboarding.BuildHelper.getSnapshotSignature; import static org.openecomp.sdc.onboarding.BuildHelper.getSourceChecksum; import static org.openecomp.sdc.onboarding.BuildHelper.readState; import static org.openecomp.sdc.onboarding.Constants.ANY_EXT; +import static org.openecomp.sdc.onboarding.Constants.CHECKSUM; import static org.openecomp.sdc.onboarding.Constants.COLON; import static org.openecomp.sdc.onboarding.Constants.DOT; import static org.openecomp.sdc.onboarding.Constants.EMPTY_JAR; @@ -47,6 +49,7 @@ import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_SOURCE_COMPILE; import static org.openecomp.sdc.onboarding.Constants.TEST; import static org.openecomp.sdc.onboarding.Constants.TEST_CHECKSUM; import static org.openecomp.sdc.onboarding.Constants.TEST_ONLY; +import static org.openecomp.sdc.onboarding.Constants.TEST_RESOURCE_CHECKSUM; import static org.openecomp.sdc.onboarding.Constants.TEST_RESOURCE_ONLY; import static org.openecomp.sdc.onboarding.Constants.TEST_SOURCE_CHECKSUM; import static org.openecomp.sdc.onboarding.Constants.UNICORN; @@ -120,6 +123,7 @@ public class PreCompileHelperMojo extends AbstractMojo { private long mainChecksum = 0; private long testChecksum = 0; private long resourceChecksum = 0; + private long testResourceChecksum = 0; Optional<String> artifactPath; static { @@ -139,9 +143,10 @@ public class PreCompileHelperMojo extends AbstractMojo { return; } resourceChecksum = getChecksum(mainResourceLocation, ANY_EXT); + testResourceChecksum = getChecksum(testResourceLocation, ANY_EXT); project.getProperties().setProperty(RESOURCE_CHECKSUM, String.valueOf(resourceChecksum)); + project.getProperties().setProperty(TEST_RESOURCE_CHECKSUM, String.valueOf(testResourceChecksum)); byte[] sourceChecksum = calculateChecksum(mainChecksum, resourceChecksum).getBytes(); - boolean instrumented = isCurrentModuleInstrumented(); artifactPath = getArtifactPathInLocalRepo(session.getLocalRepository().getUrl(), project, sourceChecksum); boolean isFirstBuild = buildState.getBuildTime(moduleCoordinates) == 0 || !artifactPath.isPresent(); @@ -150,7 +155,11 @@ public class PreCompileHelperMojo extends AbstractMojo { Map<String, Object> lastTimeModuleBuildData = buildState.readModuleBuildData(); resourceBuildData = getCurrentResourceBuildData(); Map<String, Object> lastTimeResourceBuildData = buildState.readResourceBuildData(); - + generateSyncAlert(lastTimeResourceBuildData != null && ( + !resourceBuildData.get(MAIN).equals(lastTimeResourceBuildData.get(MAIN)) || !resourceBuildData.get(TEST) + .equals(lastTimeResourceBuildData + .get(TEST) + .toString()))); boolean buildDataSameWithPreviousBuild = isBuildDataSameWithPreviousBuild(lastTimeModuleBuildData, moduleBuildData); boolean resourceMainBuildDataSameWithPreviousBuild = @@ -163,6 +172,7 @@ public class PreCompileHelperMojo extends AbstractMojo { boolean testToBeCompiled = lastTimeModuleBuildData == null || !moduleBuildData.get(TEST).equals(lastTimeModuleBuildData.get(TEST)); + final boolean instrumented = isCurrentModuleInstrumented(); setMainBuildAttribute(mainToBeCompiled, testToBeCompiled); generateSignature(sourceChecksum); setTestBuild(resourceDataSame, resourceMainBuildDataSameWithPreviousBuild, testToBeCompiled, mainToBeCompiled); @@ -182,9 +192,9 @@ public class PreCompileHelperMojo extends AbstractMojo { private void generateSignature(byte[] sourceChecksum) { try { - Paths.get(project.getBuild().getDirectory()).toFile().mkdirs(); - Files.write(Paths.get(project.getBuild().getDirectory(), project.getBuild().getFinalName() + DOT + UNICORN), - sourceChecksum, StandardOpenOption.CREATE); + Paths.get(project.getBuild().getOutputDirectory()).toFile().mkdirs(); + Files.write(Paths.get(project.getBuild().getOutputDirectory(), UNICORN + DOT + CHECKSUM), sourceChecksum, + StandardOpenOption.CREATE); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -323,7 +333,7 @@ public class PreCompileHelperMojo extends AbstractMojo { } } - private Map<String, Object> getCurrentModuleBuildData() { + private Map<String, Object> getCurrentModuleBuildData() throws MojoExecutionException { Map<String, Object> moduleBuildData = new HashMap<>(); moduleBuildData.put(MAIN, new HashMap<String, String>()); moduleBuildData.put(TEST, new HashMap<String, String>()); @@ -339,18 +349,16 @@ public class PreCompileHelperMojo extends AbstractMojo { return moduleBuildData; } for (Artifact dependency : project.getArtifacts()) { - String fileNme = dependency.getFile().getName(); + String version = dependency.isSnapshot() ? getSnapshotSignature(dependency.getFile(), + dependency.getGroupId() + COLON + dependency.getArtifactId(), dependency.getVersion()) : + dependency.getVersion(); if (excludeDependencies.contains(dependency.getScope())) { HashMap.class.cast(moduleBuildData.get(TEST)) - .put(dependency.getGroupId() + COLON + dependency.getArtifactId(), - fileNme.endsWith(dependency.getVersion() + DOT + JAR) ? dependency.getVersion() : - fileNme); + .put(dependency.getGroupId() + COLON + dependency.getArtifactId(), version); continue; } HashMap.class.cast(moduleBuildData.get(MAIN)) - .put(dependency.getGroupId() + COLON + dependency.getArtifactId(), - fileNme.endsWith(dependency.getVersion() + DOT + JAR) ? dependency.getVersion() : - fileNme); + .put(dependency.getGroupId() + COLON + dependency.getArtifactId(), version); } return moduleBuildData; } @@ -408,7 +416,7 @@ public class PreCompileHelperMojo extends AbstractMojo { private Map<String, Object> getCurrentResourceBuildData() { HashMap<String, Object> resourceBuildStateData = new HashMap<>(); resourceBuildStateData.put(MAIN, project.getProperties().getProperty(RESOURCE_CHECKSUM)); - resourceBuildStateData.put(TEST, getChecksum(testResourceLocation, ANY_EXT)); + resourceBuildStateData.put(TEST, project.getProperties().getProperty(TEST_RESOURCE_CHECKSUM)); resourceBuildStateData.put("dependency", getDependencies().hashCode()); return resourceBuildStateData; } @@ -445,6 +453,14 @@ public class PreCompileHelperMojo extends AbstractMojo { if (!checksum.equals(checksumMap.get(moduleCoordinates)) || isPMDMandatory(project.getArtifacts())) { project.getProperties().setProperty(SKIP_PMD, Boolean.FALSE.toString()); BuildState.recordPMDRun(moduleCoordinates); + generateSyncAlert(!checksum.equals(checksumMap.get(moduleCoordinates))); + } + } + + private void generateSyncAlert(boolean required) { + if (required) { + getLog().warn( + "\u001B[33m\u001B[1m UNICORN Alert!!! Source code in version control system for this module is different than your local one. \u001B[0m"); } } } |