From 12781710835588c94ebd30c21b03b040ad9bc190 Mon Sep 17 00:00:00 2001 From: Sven Oppermann Date: Thu, 22 Feb 2018 13:34:50 +0100 Subject: [PATCH] maven plugin support adjustements --- .../commandline/PacifyViaCommandline.java | 306 +++++++++--------- .../com/geewhiz/pacify/utils/FileUtils.java | 6 +- .../java/com/geewhiz/pacify/Replacer.java | 4 +- .../java/com/geewhiz/pacify/Validator.java | 207 ++++++------ 4 files changed, 264 insertions(+), 259 deletions(-) diff --git a/commandline/src/main/java/com/geewhiz/pacify/commandline/PacifyViaCommandline.java b/commandline/src/main/java/com/geewhiz/pacify/commandline/PacifyViaCommandline.java index 757de24..ada4e02 100644 --- a/commandline/src/main/java/com/geewhiz/pacify/commandline/PacifyViaCommandline.java +++ b/commandline/src/main/java/com/geewhiz/pacify/commandline/PacifyViaCommandline.java @@ -53,157 +53,157 @@ public class PacifyViaCommandline { - public static void main(String... args) { - System.err.println("PACIFy Version: " + Utils.getJarVersion()); - - int resultValue = new PacifyViaCommandline().mainInternal(args); - - System.exit(resultValue); - } - - private Logger logger = LogManager.getLogger(PacifyViaCommandline.class.getName()); - - /** - * without system.exit(). If you call it from your java app. - * - * @param args - * commandline params. - * @return - */ - public int mainInternal(String[] args) { - int resultValue = execute(args); - logger.debug("Exiting with exit code " + resultValue); - return resultValue; - } - - private int execute(String[] args) { - MainCommand mainCommand = new MainCommand(); - ReplacerCommand replacerCommand = new ReplacerCommand(); - CreatePropertyFileCommand createPropertyFileCommand = new CreatePropertyFileCommand(); - ValidateCommand validateCommand = new ValidateCommand(); - ValidateMarkerFilesCommand validateMarkerFilesCommand = new ValidateMarkerFilesCommand(); - ShowUsedPropertiesCommand showUsedPropertiesCommand = new ShowUsedPropertiesCommand(); - PreConfigureCommand preConfigureCommand = new PreConfigureCommand(); - - JCommander jc = new JCommander(mainCommand); - jc.addCommand("replace", replacerCommand); - jc.addCommand("createPropertyFile", createPropertyFileCommand); - jc.addCommand("validate", validateCommand); - jc.addCommand("validateMarkerFiles", validateMarkerFilesCommand); - jc.addCommand("showUsedProperties", showUsedPropertiesCommand); - jc.addCommand("preConfigure", preConfigureCommand); - - try { - jc.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - return 1; - } - - if (mainCommand.isDebug()) { - LoggingUtils.setLogLevel(logger, Level.DEBUG); - } else if (mainCommand.isInfo()) { - LoggingUtils.setLogLevel(logger, Level.INFO); - } else { - LoggingUtils.setLogLevel(logger, Level.ERROR); - } - - try { - if ("replace".equals(jc.getParsedCommand())) { - return executeReplacer(replacerCommand); - } else if ("createPropertyFile".equals(jc.getParsedCommand())) { - return executeCreatePropertyFile(createPropertyFileCommand); - } else if ("validate".equals(jc.getParsedCommand())) { - return executeValidate(validateCommand); - } else if ("validateMarkerFiles".equals(jc.getParsedCommand())) { - return executeValidateMarkerFiles(validateMarkerFilesCommand); - } else if ("showUsedProperties".equals(jc.getParsedCommand())) { - return executeShowUsedProperties(showUsedPropertiesCommand); - } else if ("preConfigure".equals(jc.getParsedCommand())) { - return executePreConfigure(preConfigureCommand); - } else { - jc.usage(); - if (mainCommand.isHelp()) { - return 0; - } - } - } catch (DefectRuntimeException de) { - logger.debug("We got a DefectException.", de); - return 1; - } catch (Exception e) { - logger.error("We got an Exception.", e); - return 1; - } - return 1; - } - - private int executeValidateMarkerFiles(ValidateMarkerFilesCommand validateMarkerFilesCommand) { - Validator validator = new Validator(null); - validateMarkerFilesCommand.configure(validator); - validator.execute(); - - return 0; - } - - private int executeValidate(ValidateCommand validateCommand) { - Injector injector = getInjector(validateCommand); - - Validator validator = injector.getInstance(Validator.class); - validateCommand.configure(validator); - validator.execute(); - - return 0; - } - - private int executeCreatePropertyFile(CreatePropertyFileCommand createPropertyFileCommand) { - Injector injector = getInjector(createPropertyFileCommand); - - CreatePropertyFile createPropertyFile = injector.getInstance(CreatePropertyFile.class); - createPropertyFileCommand.configure(createPropertyFile); - createPropertyFile.write(); - - return 0; - } - - private int executeReplacer(ReplacerCommand replacerCommand) { - Injector injector = getInjector(replacerCommand); - - Replacer replacer = injector.getInstance(Replacer.class); - replacerCommand.configure(replacer); - replacer.execute(); - - return 0; - } - - private int executeShowUsedProperties(ShowUsedPropertiesCommand showUsedPropertiesCommand) { - ShowUsedProperties showUsedProperties = new ShowUsedProperties(); - showUsedPropertiesCommand.configure(showUsedProperties); - showUsedProperties.execute(); - - return 0; - } - - private int executePreConfigure(PreConfigureCommand preConfigureCommand) { - Injector injector = getInjector(preConfigureCommand); - - PreConfigure preConfigure = injector.getInstance(PreConfigure.class); - preConfigureCommand.configure(preConfigure); - preConfigure.execute(); - - return 0; - } - - private Injector getInjector(BasePropertyResolverCommand command) { - List propertyResolverModules = command.getPropertyResolverModules(); - - LinkedHashSet defects = new LinkedHashSet(); - for (PropertyResolverModule propertyResolverModule : propertyResolverModules) { - defects.addAll(propertyResolverModule.getDefects()); - } - - DefectUtils.abortIfDefectExists(defects); - - Injector injector = Guice.createInjector(propertyResolverModules); - return injector; - } + public static void main(String... args) { + System.err.println("PACIFy Version: " + Utils.getJarVersion()); + + int resultValue = new PacifyViaCommandline().mainInternal(args); + + System.exit(resultValue); + } + + private Logger logger = LogManager.getLogger(PacifyViaCommandline.class.getName()); + + /** + * without system.exit(). If you call it from your java app. + * + * @param args + * commandline params. + * @return + */ + public int mainInternal(String[] args) { + int resultValue = execute(args); + logger.debug("Exiting with exit code " + resultValue); + return resultValue; + } + + private int execute(String[] args) { + MainCommand mainCommand = new MainCommand(); + ReplacerCommand replacerCommand = new ReplacerCommand(); + CreatePropertyFileCommand createPropertyFileCommand = new CreatePropertyFileCommand(); + ValidateCommand validateCommand = new ValidateCommand(); + ValidateMarkerFilesCommand validateMarkerFilesCommand = new ValidateMarkerFilesCommand(); + ShowUsedPropertiesCommand showUsedPropertiesCommand = new ShowUsedPropertiesCommand(); + PreConfigureCommand preConfigureCommand = new PreConfigureCommand(); + + JCommander jc = new JCommander(mainCommand); + jc.addCommand("replace", replacerCommand); + jc.addCommand("createPropertyFile", createPropertyFileCommand); + jc.addCommand("validate", validateCommand); + jc.addCommand("validateMarkerFiles", validateMarkerFilesCommand); + jc.addCommand("showUsedProperties", showUsedPropertiesCommand); + jc.addCommand("preConfigure", preConfigureCommand); + + try { + jc.parse(args); + } catch (ParameterException e) { + System.err.println(e.getMessage()); + return 1; + } + + if (mainCommand.isDebug()) { + LoggingUtils.setLogLevel(logger, Level.DEBUG); + } else if (mainCommand.isInfo()) { + LoggingUtils.setLogLevel(logger, Level.INFO); + } else { + LoggingUtils.setLogLevel(logger, Level.ERROR); + } + + try { + if ("replace".equals(jc.getParsedCommand())) { + return executeReplacer(replacerCommand); + } else if ("createPropertyFile".equals(jc.getParsedCommand())) { + return executeCreatePropertyFile(createPropertyFileCommand); + } else if ("validate".equals(jc.getParsedCommand())) { + return executeValidate(validateCommand); + } else if ("validateMarkerFiles".equals(jc.getParsedCommand())) { + return executeValidateMarkerFiles(validateMarkerFilesCommand); + } else if ("showUsedProperties".equals(jc.getParsedCommand())) { + return executeShowUsedProperties(showUsedPropertiesCommand); + } else if ("preConfigure".equals(jc.getParsedCommand())) { + return executePreConfigure(preConfigureCommand); + } else { + jc.usage(); + if (mainCommand.isHelp()) { + return 0; + } + } + } catch (DefectRuntimeException de) { + logger.debug("We got a DefectException.", de); + return 1; + } catch (Exception e) { + logger.error("We got an Exception.", e); + return 1; + } + return 1; + } + + private int executeValidateMarkerFiles(ValidateMarkerFilesCommand validateMarkerFilesCommand) { + Validator validator = new Validator(); + validateMarkerFilesCommand.configure(validator); + validator.execute(); + + return 0; + } + + private int executeValidate(ValidateCommand validateCommand) { + Injector injector = getInjector(validateCommand); + + Validator validator = injector.getInstance(Validator.class); + validateCommand.configure(validator); + validator.execute(); + + return 0; + } + + private int executeCreatePropertyFile(CreatePropertyFileCommand createPropertyFileCommand) { + Injector injector = getInjector(createPropertyFileCommand); + + CreatePropertyFile createPropertyFile = injector.getInstance(CreatePropertyFile.class); + createPropertyFileCommand.configure(createPropertyFile); + createPropertyFile.write(); + + return 0; + } + + private int executeReplacer(ReplacerCommand replacerCommand) { + Injector injector = getInjector(replacerCommand); + + Replacer replacer = injector.getInstance(Replacer.class); + replacerCommand.configure(replacer); + replacer.execute(); + + return 0; + } + + private int executeShowUsedProperties(ShowUsedPropertiesCommand showUsedPropertiesCommand) { + ShowUsedProperties showUsedProperties = new ShowUsedProperties(); + showUsedPropertiesCommand.configure(showUsedProperties); + showUsedProperties.execute(); + + return 0; + } + + private int executePreConfigure(PreConfigureCommand preConfigureCommand) { + Injector injector = getInjector(preConfigureCommand); + + PreConfigure preConfigure = injector.getInstance(PreConfigure.class); + preConfigureCommand.configure(preConfigure); + preConfigure.execute(); + + return 0; + } + + private Injector getInjector(BasePropertyResolverCommand command) { + List propertyResolverModules = command.getPropertyResolverModules(); + + LinkedHashSet defects = new LinkedHashSet(); + for (PropertyResolverModule propertyResolverModule : propertyResolverModules) { + defects.addAll(propertyResolverModule.getDefects()); + } + + DefectUtils.abortIfDefectExists(defects); + + Injector injector = Guice.createInjector(propertyResolverModules); + return injector; + } } diff --git a/common/src/main/java/com/geewhiz/pacify/utils/FileUtils.java b/common/src/main/java/com/geewhiz/pacify/utils/FileUtils.java index 312fa44..f43d091 100644 --- a/common/src/main/java/com/geewhiz/pacify/utils/FileUtils.java +++ b/common/src/main/java/com/geewhiz/pacify/utils/FileUtils.java @@ -93,7 +93,7 @@ public static void setPosixPermissions(Set permissions, Fil if (!IS_POSIX) { return; } - + try { Files.setPosixFilePermissions(Paths.get(forFile.toURI()), permissions); } catch (IOException e) { @@ -140,9 +140,9 @@ public static Boolean matches(Path relativePath, String regEx) { public static void copyDirectory(File sourceDir, File targetDir) throws IOException { if (sourceDir.isDirectory()) { - copyDirectoryRecursively(sourceDir, targetDir); + copyDirectoryRecursively(sourceDir.getAbsoluteFile(), targetDir.getAbsoluteFile()); } else { - Files.copy(sourceDir.toPath(), targetDir.toPath(), java.nio.file.StandardCopyOption.COPY_ATTRIBUTES); + Files.copy(sourceDir.getAbsoluteFile().toPath(), targetDir.getAbsoluteFile().toPath(), java.nio.file.StandardCopyOption.COPY_ATTRIBUTES); } } diff --git a/impl/src/main/java/com/geewhiz/pacify/Replacer.java b/impl/src/main/java/com/geewhiz/pacify/Replacer.java index 88547cb..cd28cce 100644 --- a/impl/src/main/java/com/geewhiz/pacify/Replacer.java +++ b/impl/src/main/java/com/geewhiz/pacify/Replacer.java @@ -125,8 +125,10 @@ private File createCopy() throws DefectException { throw new DefectMessage( "Destination directory [" + getCopyDestination().getAbsolutePath() + "] is not writable."); } + } else { + getCopyDestination().mkdirs(); } - + FileUtils.copyDirectory(getPackagePath(), getCopyDestination()); return getCopyDestination(); } catch (IOException e) { diff --git a/impl/src/main/java/com/geewhiz/pacify/Validator.java b/impl/src/main/java/com/geewhiz/pacify/Validator.java index 036155f..5835882 100644 --- a/impl/src/main/java/com/geewhiz/pacify/Validator.java +++ b/impl/src/main/java/com/geewhiz/pacify/Validator.java @@ -49,107 +49,110 @@ public class Validator { - private Logger logger = LogManager.getLogger(Validator.class.getName()); - - File packagePath; - List checks = new ArrayList(); - List pMarkerChecks = new ArrayList(); - - private PropertyResolveManager propertyResolveManager; - - private EntityManager entityManager; - - @Inject - public Validator(PropertyResolveManager propertyResolveManager) { - this.propertyResolveManager = propertyResolveManager; - } - - public void enablePropertyResolveChecks() { - addCheck(new CheckPropertyDuplicateInPropertyFile(propertyResolveManager)); - addPMarkerCheck(new CheckPropertyExists(propertyResolveManager)); - } - - public void enableMarkerFileChecks() { - addPMarkerCheck(new CheckArchiveDuplicateDefinedInPacifyFile()); - addPMarkerCheck(new CheckCorrectArchiveType()); - addPMarkerCheck(new CheckFileDuplicateDefinedInPacifyFile()); - addPMarkerCheck(new CheckPropertyDuplicateDefinedInPacifyFile()); - addPMarkerCheck(new CheckCorrectPacifyFilter()); - addPMarkerCheck(new CheckTargetFileExist()); - addPMarkerCheck(new CheckPlaceholderExistsInTargetFile()); - } - - public void addCheck(Check check) { - checks.add(check); - } - - public void addPMarkerCheck(PMarkerCheck check) { - pMarkerChecks.add(check); - } - - public List getChecks() { - return checks; - } - - public List getPMarkerChecks() { - return pMarkerChecks; - } - - public File getPackagePath() { - return packagePath; - } - - public void setPackagePath(File packagePath) { - this.packagePath = packagePath; - } - - public void execute() { - logger.info("== Executing Validator [Version={}]", Utils.getJarVersion()); - logger.info(" [PackagePath={}]", getPackagePath().getAbsolutePath()); - - DefectUtils.abortIfDefectExists(getEntityManager().initialize()); - - logger.info("== Found [{}] pacify marker files", getEntityManager().getPMarkerCount()); - logger.info("== Validating ..."); - - DefectUtils.abortIfDefectExists(validateInternal()); - - logger.info("== Successfully finished"); - } - - public LinkedHashSet validateInternal() { - LinkedHashSet defects = new LinkedHashSet(); - - defects.addAll(getEntityManager().initialize()); - - for (Check check : checks) { - logger.debug(" Check [{}]", check.getClass().getName()); - defects.addAll(check.checkForErrors()); - } - - for (PMarker pMarker : getEntityManager().getPMarkers()) { - logger.info(" Processing Marker File [{}]", pMarker.getFile().getAbsolutePath()); - for (PMarkerCheck pMarkerCheck : pMarkerChecks) { - logger.debug(" Check [{}]", pMarkerCheck.getClass().getName()); - defects.addAll(pMarkerCheck.checkForErrors(getEntityManager(), pMarker)); - } - } - return defects; - } - - public void setEntityManager(EntityManager entityManager) { - this.entityManager = entityManager; - } - - public EntityManager getEntityManager() { - if (entityManager == null) { - entityManager = createEntityManager(); - } - return entityManager; - } - - protected EntityManager createEntityManager() { - return new EntityManager(getPackagePath()); - } + private Logger logger = LogManager.getLogger(Validator.class.getName()); + + File packagePath; + List checks = new ArrayList(); + List pMarkerChecks = new ArrayList(); + + private PropertyResolveManager propertyResolveManager; + + private EntityManager entityManager; + + public Validator() { + } + + @Inject + public Validator(PropertyResolveManager propertyResolveManager) { + this.propertyResolveManager = propertyResolveManager; + } + + public void enablePropertyResolveChecks() { + addCheck(new CheckPropertyDuplicateInPropertyFile(propertyResolveManager)); + addPMarkerCheck(new CheckPropertyExists(propertyResolveManager)); + } + + public void enableMarkerFileChecks() { + addPMarkerCheck(new CheckArchiveDuplicateDefinedInPacifyFile()); + addPMarkerCheck(new CheckCorrectArchiveType()); + addPMarkerCheck(new CheckFileDuplicateDefinedInPacifyFile()); + addPMarkerCheck(new CheckPropertyDuplicateDefinedInPacifyFile()); + addPMarkerCheck(new CheckCorrectPacifyFilter()); + addPMarkerCheck(new CheckTargetFileExist()); + addPMarkerCheck(new CheckPlaceholderExistsInTargetFile()); + } + + public void addCheck(Check check) { + checks.add(check); + } + + public void addPMarkerCheck(PMarkerCheck check) { + pMarkerChecks.add(check); + } + + public List getChecks() { + return checks; + } + + public List getPMarkerChecks() { + return pMarkerChecks; + } + + public File getPackagePath() { + return packagePath; + } + + public void setPackagePath(File packagePath) { + this.packagePath = packagePath; + } + + public void execute() { + logger.info("== Executing Validator [Version={}]", Utils.getJarVersion()); + logger.info(" [PackagePath={}]", getPackagePath().getAbsolutePath()); + + DefectUtils.abortIfDefectExists(getEntityManager().initialize()); + + logger.info("== Found [{}] pacify marker files", getEntityManager().getPMarkerCount()); + logger.info("== Validating ..."); + + DefectUtils.abortIfDefectExists(validateInternal()); + + logger.info("== Successfully finished"); + } + + public LinkedHashSet validateInternal() { + LinkedHashSet defects = new LinkedHashSet(); + + defects.addAll(getEntityManager().initialize()); + + for (Check check : checks) { + logger.debug(" Check [{}]", check.getClass().getName()); + defects.addAll(check.checkForErrors()); + } + + for (PMarker pMarker : getEntityManager().getPMarkers()) { + logger.info(" Processing Marker File [{}]", pMarker.getFile().getAbsolutePath()); + for (PMarkerCheck pMarkerCheck : pMarkerChecks) { + logger.debug(" Check [{}]", pMarkerCheck.getClass().getName()); + defects.addAll(pMarkerCheck.checkForErrors(getEntityManager(), pMarker)); + } + } + return defects; + } + + public void setEntityManager(EntityManager entityManager) { + this.entityManager = entityManager; + } + + public EntityManager getEntityManager() { + if (entityManager == null) { + entityManager = createEntityManager(); + } + return entityManager; + } + + protected EntityManager createEntityManager() { + return new EntityManager(getPackagePath()); + } }