如何为Eclipse&M2E的maven pom.xml文件中的类路径条目定义访问规则?

Ste*_*fan 4 java eclipse javafx maven m2e

为了避免对jfxrt.jar的非法访问警告,我手动将类路径文件更改为包括访问规则:

想要的类路径条目

<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>
Run Code Online (Sandbox Code Playgroud)

如果我执行pom.xml文件,则标签accessrule会被删除,新条目为

当前由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>
Run Code Online (Sandbox Code Playgroud)

这是因为有关访问规则的信息未包含在我的pom.xml文件中。如何修改pom.xml文件以生成所需的类路径文件?

  • 我可以对Maven-compiler-plugin使用一些配置吗?

  • 还是我必须使用一些额外的Maven插件来修改类路径文本文件?

  • 还是根本无法在pom.xml文件中解决此问题,而我将不得不为M2E编写功能请求?

这是我的pom.xml文件中的一个片段(我使用pom打包):

当前的pom.xml条目用于编译阶段

<!-- ### 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>
Run Code Online (Sandbox Code Playgroud)

Ste*_*fan 5

我找到了一种解决方法来修复.classfile。需要两个步骤:

  1. 将maven-antrun-plugin添加到pom.xml文件中的#build#=>#plugins#部分,以替换.classpath文件中的文本:

<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> 
Run Code Online (Sandbox Code Playgroud)
  1. 告诉M2E执行该插件。否则,M2E将忽略它。在pom.xml文件的#build#=>#pluginManagement#部分中定义生命周期映射:

  <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>     
Run Code Online (Sandbox Code Playgroud)