private void checkExisting(
      File targetFile, ClassLoader classLoader, Document doc, Set<String> entityClasses) {
    if (targetFile.exists()) {
      final Set<String> alreadyDefined = PersistenceXmlHelper.getClassesAlreadyDefined(doc);
      for (String className : alreadyDefined) {
        if (!ReflectionHelper.classExists(className, classLoader)) {
          getLog().warn("Class " + className + " defined in " + targetFile + " does not exist");
        }
      }

      if (!alreadyDefined.containsAll(entityClasses)) {
        final Set<String> undefined = new TreeSet<>();
        for (String className : entityClasses) {
          if (!alreadyDefined.contains(className)) {
            undefined.add(className);
          }
        }

        getLog()
            .warn(
                "The following classes was not defined in "
                    + targetFile
                    + " even "
                    + "though they are available on the class path: "
                    + Arrays.toString(undefined.toArray()));
      }

      // Don't add so we end up with duplicates
      entityClasses.removeAll(alreadyDefined);
    }
  }
  private void processWeaving(ClassLoader classLoader) throws MojoExecutionException {
    if (!source.exists()) {
      throw new MojoExecutionException("Source directory " + source + " does not exist");
    }

    try {
      if (prefix != null) {
        getLog().info("Using package prefix '" + prefix + "'");
      }
      final URL[] classPath = getClassPath();
      getLog().debug("Scanning class-path: " + Arrays.toString(classPath));

      final AnnotationDB db = new AnnotationDB();
      db.setIgnoredPackages(getIgnoredPackages());
      db.scanArchives(classPath);
      final Set<String> entityClasses = findEntities(db);
      getLog().info("Entities found : " + entityClasses.size());

      final File targetFile = new File(this.target + "/META-INF/persistence.xml");
      getLog().info("Target file: " + targetFile);

      final String name = project.getArtifactId();
      final Document doc =
          targetFile.exists()
              ? PersistenceXmlHelper.parseXml(targetFile)
              : PersistenceXmlHelper.createXml(name);

      checkExisting(targetFile, classLoader, doc, entityClasses);

      PersistenceXmlHelper.appendClasses(doc, entityClasses);
      PersistenceXmlHelper.outputXml(doc, targetFile);

      final StaticWeaveProcessor weaveProcessor = new StaticWeaveProcessor(source, target);
      weaveProcessor.setClassLoader(classLoader);
      weaveProcessor.setLog(new PrintWriter(System.out));
      weaveProcessor.setLogLevel(getLogLevel());
      weaveProcessor.performWeaving();
    } catch (URISyntaxException | IOException e) {
      throw new MojoExecutionException("Error", e);
    }
  }