Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle, rt.jar access restriction

I am using a Gradle build that contains among other things:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'

repositories {
  flatDir { dirs "${System.env.JAVA_HOME}/jre/lib" }
}

dependencies {
  compile name: 'rt' 
}

It builds, that's great, yet the eclipse plugin is giving me a hard time on the rt.jar classes:

Access restriction: The type XMLSerializer is not accessible due to restriction on required library /usr/local/apps/jdk1.8.0_11/jre/lib/rt.jar

Which I understand.

Now I know you will tell me to just not use those classes. But you know how it is, I'm working on an ancient project and I just need to make it work for now.

My first question is: where is that restriction info located at?

And obviously: How can I get around that? I'm thinking of uploading it as a artifact on my Nexus repo, anything easier than that?

like image 771
benji Avatar asked Apr 15 '16 00:04

benji


1 Answers

Explanation: There is an eclipse plugin in gradle that allows for modifying the behavior for generating eclipse configs from the gradle model. This plugin has one sub-set-feature called classpath allowing to modify the generated .classpath file of eclipse.

Code:

import org.gradle.plugins.ide.eclipse.model.AccessRule

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
    classpath {
        file {
            whenMerged {
                def jre = entries.find { it.path.contains 'org.eclipse.jdt.launching.JRE_CONTAINER' }
                jre.accessRules.add(new AccessRule('0', 'com/**'))
                jre.accessRules.add(new AccessRule('0', 'sun/**'))
            }
        }
    }
}

From: https://discuss.gradle.org/t/buildship-1-0-18-is-now-available/19012 (section "Access Rules")

Parameters explained:

  • '0' = accessible
  • '1' = nonaccessible
  • '2' = discouraged

My personal usage looks like this:

eclipse.classpath {
    file.whenMerged {
        entries.each { source -> 
            if (source.kind == 'con' && source.path.startsWith('org.eclipse.jdt.launching.JRE_CONTAINER')) {
                source.accessRules.add(new AccessRule('0', 'sun/applet/AppletAudioClip'))
                source.accessRules.add(new AccessRule('0', 'javax/swing/**'))
                source.accessRules.add(new AccessRule('0', 'com/sun/java/swing/**'))
                source.accessRules.add(new AccessRule('0', 'javafx/**'))
                source.accessRules.add(new AccessRule('0', 'sun/net/www/protocol/**'))
            }
        }
    }
}

And the output is this:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/">
    <accessrules>
        <accessrule kind="accessible" pattern="sun/applet/AppletAudioClip"/>
        <accessrule kind="accessible" pattern="javax/swing/**"/>
        <accessrule kind="accessible" pattern="com/sun/java/swing/**"/>
        <accessrule kind="accessible" pattern="javafx/**"/>
        <accessrule kind="accessible" pattern="sun/net/www/protocol/**"/>
    </accessrules>
</classpathentry>

Tested & worked with:

  • Eclipse Version: Oxygen Release Candidate 3 (4.7.0 RC3)
  • Gradle Version: Gradle 3.5.1
like image 188
Frederic Leitenberger Avatar answered Nov 04 '22 08:11

Frederic Leitenberger