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

标签 java eclipse maven javafx 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>

如果我执行我的 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>

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

关于java - 如何在 Eclipse 和 M2E 的 Maven pom.xml 文件中定义类路径条目的访问规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565193/

相关文章:

java - JTable 键绑定(bind)

java - Android 在内部与外部导入库的好处

mysql - 如何使用虚拟实体数据预填充数据库?

java - 在没有版本更改的情况下检测不兼容的 API 更改

java - 如何在已经扩展 Thread 的类中扩展 JFrame

java - 如何在 Eclipse 上使用我的 java 类文件更新外部 jar

Java - 使用 exec() 方法运行另一个进程

Java 类加载器和 getResourceAsStream

java - 如何让 @ParametersAreNonnullByDefault 工作?

java - Java Classloader 能否重写系统类(仅它们的副本)的字节码?