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

2023-11-22

为了避免 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>

如果我执行 pom.xml 文件,标记访问规则已删除新条目是

当前由 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>

这是因为有关访问规则的信息未包含在我的 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>

我找到了修复我的 .class 文件的解决方法。需要两个步骤:

  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> 
  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>     
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐