6

In order to avoid illegal access warnings for jfxrt.jar I manually changed my classpath file to include access rules:

Wanted classpath entry

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
        <accessrules>
            <accessrule kind="accessible" pattern="javafx/**"/>
            <accessrule kind="accessible" pattern="com/sun/javafx/**"/>         
        </accessrules>
</classpathentry>

If I execute my pom.xml file, the tag accessrule is removed and the new entry is

Classpath entry currently produced by pom.xml / M2E

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
</classpathentry>

This is because the information about the access rules is not included in my pom.xml file. How can I modify my pom.xml file to produce the wanted classpath file?

  • Can I do so using some configuration for the maven-compiler-plugin?

  • Or do I have to use some extra maven plugin to modify the classpath text file?

  • Or is it not possible to address this in the pom.xml file at all and I would have to write a feature request for M2E?

Here is a snippet from my pom.xml file (I use pom packaging):

Curent pom.xml entry for compile phase

<!-- ### COMPILE ### phase -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <!-- specify current java version here: -->
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <id>compile-execution</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>                           
            </goals>
        </execution>
        <execution>
            <id>org.treez.test-compile-execution</id>
            <phase>org.treez.test-compile</phase>
            <goals>                         
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>       
</plugin>
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • See http://stackoverflow.com/questions/22812488/using-javafx-in-jre-8/22815472#22815472 There are several good answers to that question: mine shows how to override the access control restrictions (without using e(fx)clipse). – James_D Sep 14 '15 at 13:19
  • Thank you for linking that thread. I also wrote an answer there (currently the last one). If I understood your answer right, it shows how to edit the classpath file using wizards instead of manually editing it? Unfortunately I do not understand how that thread could answers my question. Executing the pom.xml file in a maven project with M2E will undo the manual changes from your suggested solution. And that issue is the starting point for my question here ... Please let me know if I missed something. – Stefan Sep 14 '15 at 14:03
  • Ah, ok yes. But AIUI the access control restrictions really only apply to Eclipse; so it shouldn't matter after compilation. Maybe my understanding there is wrong? – James_D Sep 14 '15 at 14:35
  • If I would completely disable the Eclipse warning on access restrictions the code would compile, I guess... and I would neither be warned for any other "illegal" access. The classpath entry shown at the beginning of my question would be just fine for me ... if M2E would leave it as is. – Stefan Sep 14 '15 at 17:40

2 Answers2

5

I found a workaround to repair my .classfile. Two steps were required:

  1. Add maven-antrun-plugin to the section #build#=>#plugins# in the pom.xml file to replace text in the .classpath file:

<plugin>                
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <configuration>
                <tasks>
                  <replace 
                    token= "JavaSE-1.8&#34;&#62;" 
                    value="JavaSE-1.8&#34;&#62;&#13; &#60;accessrules&#62;&#13; &#60;accessrule kind=&#34;accessible&#34; pattern=&#34;javafx/**&#34;/&#62;&#13; &#60;accessrule kind=&#34;accessible&#34; pattern=&#34;com/sun/javafx/**&#34;/&#62;&#13;&#60;/accessrules&#62;" 
                    file=".classpath"
                  > 
                  </replace>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin> 
  1. Tell M2E to execute that plugin. Otherwise M2E would ignore it. Define a lifecycle mapping in the #build#=>#pluginManagement# section of the pom.xml file:

  <pluginManagement>
    <plugins>
        <plugin>
             <groupId>org.eclipse.m2e</groupId>
             <artifactId>lifecycle-mapping</artifactId>
             <version>1.0.0</version>
             <configuration>
                 <lifecycleMappingMetadata>
                <pluginExecutions>
                      <pluginExecution>
                          <pluginExecutionFilter>                                               
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-antrun-plugin</artifactId>
                            <versionRange>1.8</versionRange>                                            
                            <goals>
                              <goal>run</goal>
                            </goals>
                          </pluginExecutionFilter>
                          <action>
                              <execute>
                                <runOnIncremental>false</runOnIncremental>
                              </execute>
                          </action>                                  
                     </pluginExecution>
                  </pluginExecutions>
                 </lifecycleMappingMetadata>
              </configuration>
        </plugin>    
    </plugins>
</pluginManagement>     
Stefan
  • 10,010
  • 7
  • 61
  • 117
3

You are going at it the wrong way. The problem does not come from Maven but from Eclipse.

You should install the e(fx)clipse plugin. This will remove the illegal access warnings of Eclipse. There is no need to tweak the Maven configuration; just make sure you are compiling for JDK 8.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 1
    The e(fx)clipse plugin produces unwanted css warnings (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=475347). This is why I don't want to use it for the time beeing. – Stefan Sep 14 '15 at 12:58
  • @Stefan This is a separate concern IMO. Btw, I don't know if it's actually possible to configure m2e to change Eclipse accessrules, it sounds weird. – Tunaki Sep 14 '15 at 13:23