From 03205dad582ffa5db54187f451b9a29d3d0af386 Mon Sep 17 00:00:00 2001 From: Gautam Shah Date: Fri, 1 Jun 2018 08:38:32 +0530 Subject: Fixing Merge Build issue and Sonar issues. fixing build issue and Sonar issues. Change-Id: I4209b79ab2a3646839df50be4b58f8f230b6d710 Issue-ID: SDC-1189 Signed-off-by: GAUTAMS --- openecomp-be/pom.xml | 50 +------ .../sdc/onboarding/util/ArtifactHelper.java | 118 +++++++++++++-- .../onboarding/util/CalibrateArtifactPlugin.java | 36 +---- .../sdc/onboarding/util/CopyArtifactPlugin.java | 158 --------------------- .../onboarding/util/InitializationHelperMojo.java | 114 +++++++++++---- openecomp-be/tools/build-data-helper/pom.xml | 88 ------------ openecomp-be/tools/build-data-installer/pom.xml | 30 +++- .../org/openecomp/sdc/onboarding/BuildHelper.java | 58 +++++++- .../org/openecomp/sdc/onboarding/BuildState.java | 72 ++++++++-- .../org/openecomp/sdc/onboarding/Constants.java | 5 +- .../sdc/onboarding/InitializationHelperMojo.java | 5 +- .../sdc/onboarding/PreCompileHelperMojo.java | 44 ++++-- .../sdc/onboarding/pmd/VerifyHelperMojo.java | 92 +++++++----- 13 files changed, 440 insertions(+), 430 deletions(-) delete mode 100644 openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java delete mode 100644 openecomp-be/tools/build-data-helper/pom.xml (limited to 'openecomp-be') diff --git a/openecomp-be/pom.xml b/openecomp-be/pom.xml index 047905b511..492d0e0a28 100644 --- a/openecomp-be/pom.xml +++ b/openecomp-be/pom.xml @@ -76,7 +76,7 @@ init-artifact-helper - copy-helper + calibrate-artifact-helper @@ -85,55 +85,18 @@ org.openecomp.sdc build-data-installer ${project.version} - build-data-helper - build-data-helper-${project.version}.jar ${project} + ${session} + org.openecomp.sdc:sdc-onboarding/target/build-data - - org.apache.maven.plugins - maven-dependency-plugin - ${maven.dependency.version} - - - id1 - - get - - pre-clean - - build-data-helper - - - - id2 - - get - - pre-clean - - build-data-installer - - - - - org.openecomp.sdc - ${project.version} - ${skipGet} - - org.openecomp.sdc.onboarding compile-helper-plugin ${project.version} - - org.openecomp.sdc - build-data-helper - ${project.version} - org.openecomp.sdc.onboarding pmd-helper-plugin @@ -217,13 +180,6 @@ org.openecomp.sdc.onboarding pmd-helper-plugin ${project.version} - - - org.openecomp.sdc - build-data-helper - ${project.version} - - diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java index e7ad5dacb1..f51a8aae60 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java @@ -1,21 +1,68 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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 a "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. + */ + package org.openecomp.sdc.onboarding.util; +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.io.OutputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Scanner; +import java.util.Set; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; public class ArtifactHelper { private MavenProject project; + private MavenSession session; + private static Map store = new HashMap<>(); + private static Set terminalModuleCoordinates = new HashSet<>(); + private String unicornRoot = null; + private static File unicornMetaLocation = null; + private File tempLocation = Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile(); + private static int snapshotBuildNumber = 0; + + void init(String terminalModuleCoordinate) { + setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project)); + setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1)); + terminalModuleCoordinates.add(terminalModuleCoordinate); + } + + private static void setUnicornMetaLocation(File file) { + unicornMetaLocation = file; + } + + static void setSnapshotBuildNumber(int number) { + snapshotBuildNumber = number; + } List getRepositories(boolean snapshotRepo) { List list = new ArrayList<>(); @@ -33,24 +80,75 @@ public class ArtifactHelper { return list; } + private void setTerminalModuleCoordinates(MavenProject project) { + terminalModuleCoordinates.add(getModuleCoordinate(project)); + } + + private boolean isModuleTerminal(MavenProject project) { + return terminalModuleCoordinates.contains(getModuleCoordinate(project)); + } + + File getUnicornMetaLocation() { + return unicornMetaLocation; + } + String getContents(URL path) throws IOException { try (InputStream is = path.openStream(); Scanner scnr = new Scanner(is).useDelimiter("\\A")) { return scnr.hasNext() ? scnr.next() : ""; } } - String getChecksum(String filePath, String hashType) throws NoSuchAlgorithmException, IOException { - MessageDigest md = MessageDigest.getInstance(hashType); - md.update(Files.readAllBytes(Paths.get(filePath))); - byte[] hashBytes = md.digest(); + void store(String artifactId, byte[] data) { + store.put(artifactId, data); + } - StringBuffer buffer = new StringBuffer(); - for (byte hashByte : hashBytes) { - buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1)); + void deleteAll(File f) { + if (!f.exists() && !f.isDirectory()) { + return; } - return buffer.toString(); + for (File file : f.listFiles()) { + if (f.isFile()) { + file.delete(); + } + } + } + + String getModuleCoordinate(MavenProject project) { + return project.getGroupId() + ":" + project.getArtifactId(); + } + + private File getUnicornRootFile(String moduleCoordinate, MavenProject proj) { + return getStateFile(moduleCoordinate, proj, unicornRoot); + } + + private File getStateFile(String moduleCoordinate, MavenProject proj, String filePath) { + return new File(getTopParentProject(moduleCoordinate, proj).getBasedir(), + filePath.substring(filePath.indexOf('/') + 1)); } + MavenProject getTopParentProject(String moduleCoordinate, MavenProject proj) { + if (getModuleCoordinate(proj).equals(moduleCoordinate) || proj.getParent() == null) { + return proj; + } else { + return getTopParentProject(moduleCoordinate, proj.getParent()); + } + } + + void shutDown(MavenProject project) throws IOException, ClassNotFoundException { + File file = new File(unicornMetaLocation, "compileState.dat"); + Map dataStore = null; + if (isModuleTerminal(project) && file.exists()) { + try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) { + dataStore = HashMap.class.cast(ois.readObject()); + dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber); + } + try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) { + oos.writeObject(dataStore); + } + Files.copy(file.toPath(), Paths.get(tempLocation.getAbsolutePath(), file.getName()), + StandardCopyOption.REPLACE_EXISTING); + } + } } diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java index 4838608835..96f29d0054 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java @@ -2,10 +2,6 @@ package org.openecomp.sdc.onboarding.util; import java.io.File; import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -37,46 +33,26 @@ public class CalibrateArtifactPlugin extends AbstractMojo { @Parameter private String version; @Parameter - private String targetLocation; - @Parameter - private String name; - @Parameter private String excludePackaging; @Parameter private ArtifactHelper artifactHelper; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (project.getPackaging().equals(excludePackaging)) { return; } if (project.getProperties().containsKey(ARTIFACT_COPY_PATH) && project.getProperties().getProperty(ARTIFACT_COPY_PATH) != null) { - File f = null; - String artifactPath = project.getProperties().getProperty(ARTIFACT_COPY_PATH) - .startsWith(session.getLocalRepository().getBasedir()) ? - project.getProperties().getProperty(ARTIFACT_COPY_PATH) : - project.getProperties().getProperty(ARTIFACT_COPY_PATH) - .replace(groupId, groupId.replace('.', '/')); - if (artifactPath.startsWith(session.getLocalRepository().getBasedir())) { - f = new File(artifactPath); - } else { - f = new File(session.getLocalRepository().getBasedir(), artifactPath); - } + File f = new File(project.getProperties().getProperty(ARTIFACT_COPY_PATH)); if (f.exists()) { project.getArtifact().setFile(f); } } - File file = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".unicorn"); - if (file.exists()) { - try { - Files.copy(file.toPath(), Paths.get( - session.getLocalRepository().getBasedir() + File.separator + project.getGroupId().replace(".", - File.separator) + File.separator + project.getArtifactId() + File.separator - + project.getVersion(), project.getBuild().getFinalName() + ".unicorn"), - StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - throw new UncheckedIOException(e); - } + try { + artifactHelper.shutDown(project); + } catch (IOException | ClassNotFoundException e) { + throw new MojoExecutionException("Unexpected Error Occured during shutdown activities", e); } } } diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java deleted file mode 100644 index c89fdd2a4e..0000000000 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright © 2018 European Support Limited - * - * 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 a "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. - */ - -package org.openecomp.sdc.onboarding.util; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.nio.file.StandardOpenOption; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.util.List; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.plugins.annotations.ResolutionScope; -import org.apache.maven.project.MavenProject; - -@Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN, - requiresDependencyResolution = ResolutionScope.NONE) -public class CopyArtifactPlugin extends AbstractMojo { - - @Parameter(defaultValue = "${session}") - private MavenSession session; - @Parameter(defaultValue = "${project}", readonly = true) - private MavenProject project; - @Parameter - private String groupId; - @Parameter - private String artifactId; - @Parameter - private String version; - @Parameter - private String targetLocation; - @Parameter - private String name; - @Parameter - private ArtifactHelper artifactHelper; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (!project.getProperties().containsKey("resolvedVersion")) { - return; - } - boolean isSnapshot = version.contains("SNAPSHOT"); - List artRepoList = artifactHelper.getRepositories(isSnapshot); - String resolvedVersion = project.getProperties().getProperty("resolvedVersion"); - try { - if (!version.equals(resolvedVersion)) { - boolean result = copyResolvedArtifact(artRepoList, resolvedVersion); - if (result && getLog().isInfoEnabled()) { - getLog().info("Data Artifact Copied with " + resolvedVersion); - } - - } - File orgFile = new File( - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version); - if (!orgFile.exists()) { - return; - } - File[] list = orgFile.listFiles(t -> t.getName().equals(artifactId + "-" + version + ".jar")); - if (list != null && list.length > 0) { - String directory = session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", - File.separator)) + File.separator + targetLocation + File.separator + version; - if (!Paths.get(directory, name).toFile().exists()) { - return; - } - Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING); - copyTargetArtifact(directory, list[0]); - } - } catch (IOException | NoSuchAlgorithmException e) { - throw new MojoFailureException(e.getMessage(), e); - } - } - - private void copyTargetArtifact(String directory, File source) throws IOException, NoSuchAlgorithmException { - File[] files = new File(directory).listFiles( - f -> f.getName().endsWith(".jar") && !f.getName().equals(name) && f.getName().startsWith( - name.substring(0, name.lastIndexOf('-')))); - if (files == null || files.length == 0) { - return; - } - Arrays.sort(files, this::compare); - File tgtFile = files[files.length - 1]; - Files.copy(source.toPath(), tgtFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - for (String checksumType : Arrays.asList("sha1", "md5")) { - File potentialFile = new File(tgtFile.getAbsolutePath() + "." + checksumType); - if (potentialFile.exists()) { - Files.write(potentialFile.toPath(), - artifactHelper.getChecksum(source.getAbsolutePath(), checksumType).getBytes(), - StandardOpenOption.CREATE); - } - } - } - - - private boolean copyResolvedArtifact(List list, String resolvedVersion) { - for (ArtifactRepository repo : list) { - try { - writeContents( - new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/' - + artifactId + "-" + (version.equals(resolvedVersion) ? version : - version.replace("SNAPSHOT", resolvedVersion)) - + ".jar")); - return true; - } catch (IOException e) { - getLog().debug(e); - } - } - return false; - } - - - private void writeContents(URL path) throws IOException { - String directory = - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version; - try (InputStream is = path.openStream()) { - Files.copy(is, Paths.get(directory, artifactId + "-" + version + ".jar"), - StandardCopyOption.REPLACE_EXISTING); - } - - } - - private int compare(File file1, File file2) { - if (file1.lastModified() > file2.lastModified()) { - return 1; - } - if (file1.lastModified() < file2.lastModified()) { - return -1; - } - return 0; - } - -} diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java index 22ba506c48..6fa9b0e365 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java @@ -18,7 +18,12 @@ package org.openecomp.sdc.onboarding.util; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,9 +43,11 @@ import org.apache.maven.settings.Proxy; requiresDependencyResolution = ResolutionScope.NONE) public class InitializationHelperMojo extends AbstractMojo { - private static final String SKIP_GET = "skipGet"; + private static final String UNICORN_INITIALIZED = "unicorn_initialized"; private static final String HTTP = "http"; private static final String HTTPS = "https"; + private static final String SNAPSHOT = "SNAPSHOT"; + private static final String DOT = "."; @Parameter(defaultValue = "${session}") private MavenSession session; @@ -53,42 +60,36 @@ public class InitializationHelperMojo extends AbstractMojo { @Parameter private String version; @Parameter - private String targetLocation; - @Parameter - private String name; - @Parameter private String excludePackaging; @Parameter private ArtifactHelper artifactHelper; @Override public void execute() throws MojoExecutionException, MojoFailureException { - if (System.getProperties().containsKey(SKIP_GET)) { - project.getProperties() - .setProperty(SKIP_GET, Boolean.toString(System.getProperties().containsKey(SKIP_GET))); + if (System.getProperties().containsKey(UNICORN_INITIALIZED)) { return; - } else { - File orgFile = new File( - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version); - String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT"))); - project.getProperties().setProperty("resolvedVersion", resolvedVersion); - System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); - if (resolvedVersion.equals(version) && !orgFile.exists()) { - project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); - } } + artifactHelper.init(groupId + ":" + artifactId); + artifactHelper.deleteAll(artifactHelper.getUnicornMetaLocation()); + String resolvedVersion = + getResolvedVersion(artifactHelper.getRepositories(version.contains(SNAPSHOT)), artifactId); + getLog().info(resolvedVersion.equals(version) ? "Unicorn Initialization Failed!!!" : + "Unicorn Initialization Completed Successfully!!!"); + System.getProperties().setProperty(UNICORN_INITIALIZED, Boolean.TRUE.toString()); } - private String getResolvedVersion(List list) { + private String getResolvedVersion(List list, String artifactId) { Pattern timestampPattern = Pattern.compile(".*(.*).*"); Pattern buildNumberPattern = Pattern.compile(".*(.*).*"); + String timestamp = null; String buildNumber = null; for (ArtifactRepository repo : list) { try { URL url = new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + "/maven-metadata.xml"); + URL fallbackUrl = + new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/'); setProxy(url); String content = artifactHelper.getContents(url); Matcher m = timestampPattern.matcher(content); @@ -99,16 +100,60 @@ public class InitializationHelperMojo extends AbstractMojo { if (m.find()) { buildNumber = m.group(1); } + timestamp = verifyBuildTimestamp(buildNumber, timestamp, fallbackUrl); + if (timestamp != null && buildNumber != null) { + byte[] data = fetchContents(repo.getUrl(), artifactId, timestamp + "-" + buildNumber); + artifactHelper.store(artifactId, data); + getLog().info(artifactId + " Version to be copied is " + timestamp + "-" + buildNumber); + artifactHelper.setSnapshotBuildNumber(Integer.parseInt(buildNumber)); + return timestamp + "-" + buildNumber; + } } catch (IOException e) { getLog().debug(e); } - if (timestamp != null && buildNumber != null) { - return timestamp + "-" + buildNumber; - } } return version; } + private String verifyBuildTimestamp(String buildNumber, String timestamp, URL fallbackUrl) throws IOException { + if (buildNumber == null) { + return timestamp; + } + String buildPage = artifactHelper.getContents(fallbackUrl); + Pattern verifyPattern = Pattern.compile( + ".*" + artifactId + "-" + version.replace(SNAPSHOT, "") + "(.*)" + "-" + buildNumber + ".jar.*"); + Matcher m = verifyPattern.matcher(buildPage); + if (m.find()) { + String str = m.group(1); + if (!str.equals(timestamp)) { + return str; + } + } + return timestamp; + } + + private byte[] fetchContents(String repoUrl, String artifactId, String resolvedVersion) throws IOException { + File location = Paths.get(project.getBuild().getDirectory(), "build-data").toFile(); + location.mkdirs(); + File file = new File(location, artifactId + "-" + (version.equals(resolvedVersion) ? version : + version.replace(SNAPSHOT, resolvedVersion)) + DOT + + "jar"); + URL path = new URL(repoUrl + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/' + artifactId + + "-" + (version.equals(resolvedVersion) ? version : + version.replace(SNAPSHOT, resolvedVersion)) + DOT + "jar"); + try (InputStream is = path.openStream()) { + Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + byte[] data = Files.readAllBytes(file.toPath()); + try { + addJarToClasspath(file); + } catch (Exception e) { + getLog().error("Error while feeding the build-data into system.", e); + } + + return data; + } + private void setProxy(URL url) { if (url.getProtocol().equalsIgnoreCase(HTTP)) { setProperties("http.proxyHost", "http.proxyPort", "http.nonProxyHosts", HTTP); @@ -121,10 +166,29 @@ public class InitializationHelperMojo extends AbstractMojo { String protocol) { for (Proxy proxy : session.getSettings().getProxies()) { if (proxy.isActive() && proxy.getProtocol().equalsIgnoreCase(protocol)) { - System.setProperty(proxyHostProperty, proxy.getHost()); - System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort())); - System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts()); + if (proxy.getHost() != null && !proxy.getHost().trim().isEmpty()) { + System.setProperty(proxyHostProperty, proxy.getHost()); + System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort())); + } + if (proxy.getNonProxyHosts() != null && !proxy.getNonProxyHosts().trim().isEmpty()) { + System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts()); + } } } } + + public void addJarToClasspath(File jar) throws MojoFailureException { + try { + ClassLoader cl = ClassLoader.getSystemClassLoader(); + Class clazz = cl.getClass(); + + Method method = clazz.getSuperclass().getDeclaredMethod("addURL", new Class[] {URL.class}); + + method.setAccessible(true); + method.invoke(cl, new Object[] {jar.toURI().toURL()}); + } catch (Exception e) { + throw new MojoFailureException("Problem while loadig build-data", e); + } + } + } diff --git a/openecomp-be/tools/build-data-helper/pom.xml b/openecomp-be/tools/build-data-helper/pom.xml deleted file mode 100644 index b639c7ae0d..0000000000 --- a/openecomp-be/tools/build-data-helper/pom.xml +++ /dev/null @@ -1,88 +0,0 @@ - - - 4.0.0 - - org.openecomp.sdc - build-data-helper - - - sdc-onboarding - org.openecomp.sdc - 1.3.0-SNAPSHOT - ../../../onboarding - - - true - ${project.groupId}/${project.artifactId}/${project.version}/${project.artifactId}-${project.version}.jar - - - - - org.openecomp.sdc.onboarding - artifact-copy-plugin - ${project.version} - - - id3 - - copy-helper - calibrate-artifact-helper - - install - - - id4 - - init-artifact-helper - - - - - org.openecomp.sdc - build-data-installer - ${project.version} - build-data-helper - build-data-helper-${project.version}.jar - - ${project} - - - - - org.apache.maven.plugins - maven-dependency-plugin - ${maven.dependency.version} - - - id1 - - get - - clean - - build-data-helper - - - - id2 - - get - - clean - - build-data-installer - - - - - org.openecomp.sdc - ${project.version} - ${skipGet} - - - - - - \ No newline at end of file diff --git a/openecomp-be/tools/build-data-installer/pom.xml b/openecomp-be/tools/build-data-installer/pom.xml index cf2c91bbf3..2d79e472e9 100644 --- a/openecomp-be/tools/build-data-installer/pom.xml +++ b/openecomp-be/tools/build-data-installer/pom.xml @@ -22,13 +22,6 @@ org.openecomp.sdc.onboarding pmd-helper-plugin ${project.version} - - - org.openecomp.sdc - build-data-helper - ${project.version} - - @@ -47,6 +40,29 @@ ${project.build.outputDirectory}/pmd.dat + + org.openecomp.sdc.onboarding + artifact-copy-plugin + ${project.version} + + + + init-artifact-helper + calibrate-artifact-helper + + + + + org.openecomp.sdc + build-data-installer + ${project.version} + + ${project} + ${session} + org.openecomp.sdc:sdc-onboarding/target/build-data + + + \ No newline at end of file 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 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 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 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 Optional readState(String fileName, Class 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 compileDataStore = new HashMap<>(); private static Map moduleBuildData = new HashMap<>(); private static Map resourceBuildData = new HashMap<>(); private static Map artifacts = new HashMap<>(); private static Set executeTestsIfDependsOnStore = new HashSet<>(); private static Set 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 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 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 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 lastTimeModuleBuildData = buildState.readModuleBuildData(); resourceBuildData = getCurrentResourceBuildData(); Map 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 getCurrentModuleBuildData() { + private Map getCurrentModuleBuildData() throws MojoExecutionException { Map moduleBuildData = new HashMap<>(); moduleBuildData.put(MAIN, new HashMap()); moduleBuildData.put(TEST, new HashMap()); @@ -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 getCurrentResourceBuildData() { HashMap 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"); } } } diff --git a/openecomp-be/tools/pmd-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/pmd/VerifyHelperMojo.java b/openecomp-be/tools/pmd-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/pmd/VerifyHelperMojo.java index 87c9ca5f1f..c80951ff50 100644 --- a/openecomp-be/tools/pmd-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/pmd/VerifyHelperMojo.java +++ b/openecomp-be/tools/pmd-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/pmd/VerifyHelperMojo.java @@ -17,9 +17,9 @@ package org.openecomp.sdc.onboarding.pmd; import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.getStateFile; +import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.isReportEmpty; import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.readCurrentPMDState; import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.writeCurrentPMDState; -import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.isReportEmpty; import java.io.File; import java.io.IOException; @@ -28,7 +28,6 @@ import java.nio.file.Files; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -78,28 +77,19 @@ public class VerifyHelperMojo extends AbstractMojo { if (project.getPackaging().equals(excludePackaging)) { return; } - if (moduleCoordinates.equals(persistingModuleCoordinates)) { - if (pmdStateFile.exists()) { - pmdStateFile.delete(); - } - } - if (pmdCurrentStateFile == null) { - pmdCurrentStateFile = - getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project, - pmdCurrentStateFilePath); - pmdCurrentStateFile.getParentFile().mkdirs(); - pmdReportFile.getParentFile().mkdirs(); - } - if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) { - getLog().error("PMD Check is skipped. problem while loading data."); - } - if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(pmdReportFile)) { + init(); + warnDataIssuesIfAny(); + + if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty( + pmdReportFile)) { Map> data = readCurrentPMDState(pmdCurrentStateFile); Map> cv = readCurrentModulePMDReport(); data.putAll(cv); + boolean error = false; if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils .evaluateCodeQuality(PMDState.getHistoricState(), cv, pmdFailureReportLocation, getLog())) { + error = true; if (validatePMDReport) { throw new MojoFailureException( "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation @@ -111,39 +101,63 @@ public class VerifyHelperMojo extends AbstractMojo { + " and address them before committing this code in Version Control System. \u001B[0m"); } } + String moduleChecksum = project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties() + .getProperty( + "testChecksum"); + data = reinitializeIfNeeded(!error, data); + Map checksumStore = HashMap.class.cast(data); - checksumStore.put(moduleCoordinates, - project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties() - .getProperty("testChecksum")); - writeCurrentPMDState(pmdCurrentStateFile, data); + if (!moduleChecksum.equals(checksumStore.get(moduleCoordinates))) { + checksumStore.put(moduleCoordinates, moduleChecksum); + writeCurrentPMDState(pmdCurrentStateFile, data); + } } if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) { - if (isReportEmpty(pmdReportFile)){ + if (isReportEmpty(pmdReportFile)) { HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile)); data.put(moduleCoordinates, - project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties() - .getProperty("testChecksum")); + project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties().getProperty( + "testChecksum")); writeCurrentPMDState(pmdCurrentStateFile, data); } pmdReportFile.delete(); } + removeProcessFiles(); + + } + + private void removeProcessFiles() { + if (moduleCoordinates.equals(persistingModuleCoordinates) && pmdStateFile.exists()) { + pmdStateFile.delete(); + } if (pmdTargetLocation.exists()) { pmdTargetLocation.delete(); } + } + + private void init() { + if (pmdCurrentStateFile == null) { + setPmdCurrentStateFile( + getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project, + pmdCurrentStateFilePath)); + + pmdReportFile.getParentFile().mkdirs(); + } + } + private static void setPmdCurrentStateFile(File file) { + pmdCurrentStateFile = file; + pmdCurrentStateFile.getParentFile().mkdirs(); } private Map> readCurrentModulePMDReport() { try { PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates); if (pmdReportFile.exists()) { - boolean isFirst = true; - for (String line : Files.lines(pmdReportFile.toPath()).collect(Collectors.toList())) { - if (isFirst) { - isFirst = false; - } else { - PMDState.addViolation(line, moduleCoordinates); - } + List lines = Files.readAllLines(pmdReportFile.toPath()); + lines.remove(0); + for (String line : lines) { + PMDState.addViolation(line, moduleCoordinates); } } } catch (IOException ioe) { @@ -152,4 +166,18 @@ public class VerifyHelperMojo extends AbstractMojo { return PMDState.getState(); } + private void warnDataIssuesIfAny() { + if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) { + getLog().error("PMD Check is skipped. problem while loading data."); + } + } + + private Map> reinitializeIfNeeded(boolean required, Map> orig) { + if (required) { + return readCurrentPMDState(pmdCurrentStateFile); + } else { + return orig; + } + } + } -- cgit 1.2.3-korg