/** @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() */
  public void write() throws MojoExecutionException {
    // check for existence
    if (!config.getManifestFile().exists()) {
      log.warn(
          Messages.getString(
              "EclipseOSGiManifestWriter.nomanifestfile",
              config.getManifestFile().getAbsolutePath()));
      return;
    }

    StringBuffer manifestSb = rewriteManifest(config.getManifestFile());
    FileWriter fos = null;
    try {
      fos = new FileWriter(config.getManifestFile());
      fos.write(manifestSb.toString());
    } catch (FileNotFoundException e) {
      throw new MojoExecutionException(
          Messages.getString("cantwritetofile", config.getManifestFile().getAbsolutePath()));
    } catch (IOException e) {
      throw new MojoExecutionException(
          Messages.getString("cantwritetofile", config.getManifestFile().getAbsolutePath()), e);
    } finally {
      IOUtil.close(fos);
    }
  }
  protected StringBuffer rewriteManifest(File manifestFile) throws MojoExecutionException {

    // warning: we read and rewrite the file line by line in order to preserve formatting
    boolean inBundleClasspathEntry = false;
    StringBuffer manifestSb = new StringBuffer();
    try {
      BufferedReader in = new BufferedReader(new FileReader(manifestFile));
      String line;
      while ((line = in.readLine()) != null) {
        if (inBundleClasspathEntry && line.indexOf(":") > -1) {
          inBundleClasspathEntry = false;
        } else if (inBundleClasspathEntry) {
          // skip it
          continue;
        }

        if (line.startsWith(ENTRY_BUNDLE_CLASSPATH)) {
          inBundleClasspathEntry = true;
        } else if (line.startsWith(ENTRY_BUNDLE_NAME)) {
          manifestSb.append(ENTRY_BUNDLE_NAME);
          manifestSb.append(" ");
          manifestSb.append(config.getProject().getName());
          manifestSb.append(NEWLINE);
        } else if (line.startsWith(ENTRY_BUNDLE_SYMBOLICNAME)) {
          manifestSb.append(ENTRY_BUNDLE_SYMBOLICNAME);
          manifestSb.append(" ");
          manifestSb.append(config.getProject().getArtifactId());
          manifestSb.append(";singleton:=true");
          manifestSb.append(NEWLINE);
        } else if (line.startsWith(ENTRY_BUNDLE_VERSION)) {
          manifestSb.append(ENTRY_BUNDLE_VERSION);
          manifestSb.append(" ");
          manifestSb.append(config.getProject().getVersion());
          manifestSb.append(NEWLINE);
        } else if (line.startsWith(ENTRY_BUNDLE_VENDOR)
            && config.getProject().getOrganization() != null) {
          manifestSb.append(ENTRY_BUNDLE_VENDOR);
          manifestSb.append(" ");
          manifestSb.append(config.getProject().getOrganization().getName());
          manifestSb.append(NEWLINE);
        } else {
          manifestSb.append(line + NEWLINE);
        }
      }

      IOUtil.close(in);
    } catch (IOException e) {
      throw new MojoExecutionException(
          Messages.getString("cantreadfile", manifestFile.getAbsolutePath()));
    }
    manifestSb.append(addBundleClasspathEntries());

    // OSGi manifest headers need to end with a line break
    manifestSb.append(NEWLINE);
    return manifestSb;
  }
  /** @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() */
  public void write() throws MojoExecutionException {

    // check if it's necessary to create project specific settings
    Properties coreSettings = new Properties();

    String source = IdeUtils.getCompilerSourceVersion(config.getProject());
    String encoding = IdeUtils.getCompilerSourceEncoding(config.getProject());
    String target = IdeUtils.getCompilerTargetVersion(config.getProject());

    if (source != null) {
      coreSettings.put(PROP_JDT_CORE_COMPILER_SOURCE, source);
      coreSettings.put(PROP_JDT_CORE_COMPILER_COMPLIANCE, source);
    }

    if (encoding != null) {
      File basedir = config.getProject().getBasedir();
      List compileSourceRoots = config.getProject().getCompileSourceRoots();
      if (compileSourceRoots != null) {
        for (Object compileSourceRoot : compileSourceRoots) {
          String sourcePath = (String) compileSourceRoot;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List testCompileSourceRoots = config.getProject().getTestCompileSourceRoots();
      if (testCompileSourceRoots != null) {
        for (Object testCompileSourceRoot : testCompileSourceRoots) {
          String sourcePath = (String) testCompileSourceRoot;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List resources = config.getProject().getResources();
      if (resources != null) {
        for (Object resource1 : resources) {
          Resource resource = (Resource) resource1;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List testResources = config.getProject().getTestResources();
      if (testResources != null) {
        for (Object testResource : testResources) {
          Resource resource = (Resource) testResource;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
    }

    if (target != null && !JDK_1_2_SOURCES.equals(target)) {
      coreSettings.put(
          "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target); // $NON-NLS-1$
    }

    // write the settings, if needed
    if (!coreSettings.isEmpty()) {
      File settingsDir =
          new File(
              config.getEclipseProjectDirectory(),
              EclipseWorkspaceWriter.DIR_DOT_SETTINGS); // $NON-NLS-1$

      settingsDir.mkdirs();

      coreSettings.put(PROP_ECLIPSE_PREFERENCES_VERSION, "1"); // $NON-NLS-1$

      try {
        File oldCoreSettingsFile;

        File coreSettingsFile =
            new File(settingsDir, EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE);

        if (coreSettingsFile.exists()) {
          oldCoreSettingsFile = coreSettingsFile;

          Properties oldsettings = new Properties();
          oldsettings.load(new FileInputStream(oldCoreSettingsFile));

          Properties newsettings = (Properties) oldsettings.clone();
          newsettings.putAll(coreSettings);

          if (!oldsettings.equals(newsettings)) {
            newsettings.store(new FileOutputStream(coreSettingsFile), null);
          }
        } else {
          coreSettings.store(new FileOutputStream(coreSettingsFile), null);

          log.info(
              Messages.getString(
                  "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
                  coreSettingsFile.getCanonicalPath()));
        }
      } catch (FileNotFoundException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.cannotcreatesettings"), e); // $NON-NLS-1$
      } catch (IOException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.errorwritingsettings"), e); // $NON-NLS-1$
      }
    } else {
      log.info(Messages.getString("EclipseSettingsWriter.usingdefaults")); // $NON-NLS-1$
    }
  }
예제 #4
0
  /** @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() */
  public void write() throws MojoExecutionException {

    // check if it's necessary to create project specific settings
    Properties ajdtSettings = new Properties();

    IdeDependency[] deps = config.getDeps();
    int ajdtDepCount = 0;
    int ajdtWeaveDepCount = 0;
    for (int i = 0; i < deps.length; i++) {
      if (deps[i].isAjdtDependency()) {
        addDependency(ajdtSettings, deps[i], ASPECT_DEP_PROP, ++ajdtDepCount);
      }

      if (deps[i].isAjdtWeaveDependency()) {
        addDependency(ajdtSettings, deps[i], WEAVE_DEP_PROP, ++ajdtWeaveDepCount);
      }
    }

    // write the settings, if needed
    if (!ajdtSettings.isEmpty()) {
      File settingsDir =
          new File(config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS); // $NON-NLS-1$

      settingsDir.mkdirs();

      ajdtSettings.put(PROP_ECLIPSE_PREFERENCES_VERSION, "1"); // $NON-NLS-1$

      try {
        File oldAjdtSettingsFile;

        File ajdtSettingsFile = new File(settingsDir, FILE_AJDT_PREFS);

        if (ajdtSettingsFile.exists()) {
          oldAjdtSettingsFile = ajdtSettingsFile;

          Properties oldsettings = new Properties();
          oldsettings.load(new FileInputStream(oldAjdtSettingsFile));

          Properties newsettings = (Properties) oldsettings.clone();
          newsettings.putAll(ajdtSettings);

          if (!oldsettings.equals(newsettings)) {
            newsettings.store(new FileOutputStream(ajdtSettingsFile), null);
          }
        } else {
          ajdtSettings.store(new FileOutputStream(ajdtSettingsFile), null);

          log.info(
              Messages.getString(
                  "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
                  ajdtSettingsFile.getCanonicalPath()));
        }
      } catch (FileNotFoundException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.cannotcreatesettings"), e); // $NON-NLS-1$
      } catch (IOException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.errorwritingsettings"), e); // $NON-NLS-1$
      }
    }
  }
예제 #5
0
  private void addDependency(Properties ajdtSettings, IdeDependency dep, String propName, int index)
      throws MojoExecutionException {

    String path;

    if (dep.isReferencedProject() && !config.isPde()) {
      path = "/" + dep.getEclipseProjectName(); // $NON-NLS-1$
    } else if (dep.isReferencedProject() && config.isPde()) {
      // don't do anything, referenced projects are automatically handled by eclipse in PDE builds
      return;
    } else {
      File artifactPath = dep.getFile();

      if (artifactPath == null) {
        log.error(
            Messages.getString("EclipsePlugin.artifactpathisnull", dep.getId())); // $NON-NLS-1$
        return;
      }

      if (dep.isSystemScoped()) {
        path =
            IdeUtils.toRelativeAndFixSeparator(
                config.getEclipseProjectDirectory(), artifactPath, false);

        if (log.isDebugEnabled()) {
          log.debug(
              Messages.getString(
                  "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$
                  new Object[] {dep.getArtifactId(), path}));
        }
      } else {
        File localRepositoryFile = new File(config.getLocalRepository().getBasedir());

        // if the dependency is not provided and the plugin runs in "pde mode", the dependency is
        // added to the Bundle-Classpath:
        if (config.isPde() && (dep.isProvided() || dep.isOsgiBundle())) {
          return;
        } else if (config.isPde() && !dep.isProvided() && !dep.isTestDependency()) {
          // path for link created in .project, not to the actual file
          path = dep.getFile().getName();
        }
        // running in PDE mode and the dependency is provided means, that it is provided by
        // the target platform. This case is covered by adding the plugin container
        else {
          String fullPath = artifactPath.getPath();
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(localRepositoryFile, new File(fullPath), false);

          if (!new File(relativePath).isAbsolute()) {
            path =
                EclipseClasspathWriter.M2_REPO
                    + "/" //$NON-NLS-1$
                    + relativePath;
          } else {
            path = relativePath;
          }
        }
      }
    }

    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY);
    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY);
    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + index, path);
  }