Modify ivy classpath entry

271 views Asked by At

In the picture below, I want

  1. Ivy settings path to NOT include ${ivyproject_loc}
  2. Property Files to NOT include ${project_loc}

Here is what I have right now. I was thinking it might be easier to remove the ivy library and add it the way I want it. I know how to add, but what is the best way to remove?

protected void changeIvyClasspath(IProject project) throws CoreException {

    if(project.hasNature(JavaCore.NATURE_ID)){
        IJavaProject jproject = JavaCore.create(project);
        IClasspathEntry[] entries = jproject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if(entry.toString().contains("org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER")){
                if(entry.toString().contains("project_loc") || entry.toString().contains("ivyproject_loc")){
                    //how to remove entry?

                    addIvyToClasspath();
                }
            }
        }
    }
}

Something else I have tried is to modify the existing settings, but it did not work.

Here is the code that attempted to do that:

protected void changeIvyClasspath(IProject project) throws CoreException {

    if(project.hasNature(JavaCore.NATURE_ID)){
        IJavaProject jproject = JavaCore.create(project);
        IClasspathEntry[] entries = jproject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if(entry.toString().contains("org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER")){
                if(entry.toString().contains("project_loc") || entry.toString().contains("ivyproject_loc")){
                    IvyClasspathContainer ivycp = IvyClasspathContainerHelper.getContainer(entry.getPath(), jproject);
                    IvyClasspathContainerConfiguration conf = new IvyClasspathContainerConfiguration(jproject, "ivy.xml", true);
                    SettingsSetup ss = conf.getIvySettingsSetup();
                    List<String> props = new ArrayList<String>();
                    props.add("project.properties");
                    props.add(".properties/eclipse.properties");
                    ss.setPropertyFiles(props);
                    conf.setIvySettingsSetup(ss);
                    ss.setIvySettingsPath(".properties/ivysettings.xml");
                    ivycp.setConf(conf);
                    ivycp.launchResolve(false, null);
                }
            }
        }
    }
}
1

There are 1 answers

0
Chris Bolton On BEST ANSWER

With the project, you can get all the class path entries. I went through and excludes the ivy entries. Then I re-added them with the correct class path and property files. Now, the paths do not include ${ivyproject_loc} and ${project_loc}.

Here is the code:

protected void changeIvyClasspath(IProject project, IProgressMonitor monitor) throws CoreException {

    if(project.hasNature(JavaCore.NATURE_ID)){
        IJavaProject jproject = JavaCore.create(project);
        List<IClasspathEntry> iClasspathEntryList = new ArrayList<IClasspathEntry>();

        IClasspathEntry[] entries = jproject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            //exclude ivy.xml entries
            if (!entry.getPath().toString().contains("ivy.xml")) {
                iClasspathEntryList.add(entry);
            } 
        }

        //Add the ivy entries with the correct class path
        addIvyToClasspath(new String[] { "compile", "runtime" }, true, iClasspathEntryList, project, jproject, monitor);
        addIvyToClasspath(new String[] { "provided", "test" }, false, iClasspathEntryList, project, jproject, monitor);

        entries = iClasspathEntryList.toArray(new IClasspathEntry[iClasspathEntryList.size()]);

        jproject.setRawClasspath(entries, jproject.getOutputLocation(), null);
    }
}