简体   繁体   English

如何将自定义JAR添加到“eclipse-pmd”插件?

[英]How to Add custom JAR to the “eclipse-pmd” plugin?

Installed the Eclipse PMD plugin (written by Philip Graf ) from here . 这里安装了Eclipse PMD插件 (由Philip Graf编写)。 I tried to associate a custom PMD rule classes JAR but couldn't find a provision for it. 我尝试关联自定义PMD规则类JAR但无法找到它的规定。 This JAR contains custom rule classes (that extends AbstractJavaRule indirectly). 此JAR包含自定义规则类(间接extends AbstractJavaRule )。

So is there any way to configure eclipse-pmd plugin to accept this custom JAR? 那么有没有办法配置eclipse-pmd插件来接受这个自定义JAR?

My custompmd.jar has this structure: 我的custompmd.jar有这样的结构:

custompmd.jar custompmd.jar
└─com/pmd/custom └─com/ PMD /自定义
└─AvoidHardcodingRule.class └─AvoidHardcodingRule.class
└─AvoidCatchWithoutLogErrorRule.class └─AvoidCatchWithoutLogErrorRule.class
└─etc... └─etc...

Simply putting it in plugins directory didn't help. 简单地将它放在plugins目录中没有帮助。 One of the SO question suggests a solution but that's for a different plugin . 其中一个问题提出了一个解决方案,但这是针对不同的 插件

[UPDATE] [UPDATE]
I am currently thinking of editing one of the eclipse\\plugins\\ch.acanda.eclipse.pmd.BLAHBLAH.jar , add custompmd.jar into the lib folder and updating META-INF\\MANIFEST.MF file by appending the JAR information in the Bundle-ClassPath: section. 我目前正在考虑编辑其中一个eclipse\\plugins\\ch.acanda.eclipse.pmd.BLAHBLAH.jar ,将custompmd.jar添加到lib文件夹中,并通过在JAR信息中附加JAR信息来更新META-INF\\MANIFEST.MF文件。 Bundle-ClassPath:部分。 Is it the right practice as patching the plugin JAR seems ugly to me? 这是正确的做法,因为修补插件JAR对我来说似乎很丑陋吗?

[UPDATE] [UPDATE]
The above approach didn't work. 上述方法不起作用。

Any of you Eclipse gurus know how to crack it? 你们中的任何一个Eclipse大师都知道如何破解它?

[UPDATE] [UPDATE]
I cracked it by myself. 我自己解决了这个问题。

[UPDATE] [UPDATE]
Although the below solution works, it is a maintenance nightmare. 虽然以下解决方案有效,但这是一场维护噩梦。 Whenever, I change the custompmd.jar , I have to update the plugin JAR as well everytime, which is tedious. 每当我更改custompmd.jar ,我每次都必须更新插件JAR,这很乏味。 Is there a clean and neat solution? 有干净整洁的解决方案吗?

Finally figured it out myself ! 终于弄清楚了自己!

(I should've got it working in my previous attempt itself, but due to a stale custompmd.jar because of failed ANT task, it didn't work that time). (我应该在我之前的尝试中使用它,但是由于ANT任务失败而导致custompmd.jar过时,但是那段时间不起作用)。

Step-by-step: 一步步:

  1. Copy the ch.acanda.eclipse.pmd.core_<blahblah#>.jar from plugins directory into a temp directory. ch.acanda.eclipse.pmd.core_<blahblah#>.jarplugins目录复制到临时目录中。
  2. Open the JAR in temp directory using 7-Zip software 使用7-Zip软件在临时目录中打开JAR
  3. Add custompmd.jar into the lib directory. custompmd.jar添加到lib目录中。
  4. Edit META-INF/MANIFEST.MF file, append this string ,lib/custompmd.jar at the end of Bundle-Classpath: 编辑META-INF/MANIFEST.MF文件,在Bundle-Classpath:末尾附加此字符串,lib/custompmd.jar Bundle-Classpath:
  5. Close 7-Zip (and accept the Save dialog). 关闭7-Zip(并接受“保存”对话框)。
  6. Take a backup of the existing /plugins/ch.acanda.eclipse.pmd.core_<blahblah#>.jar and replace it with this temp jar. 备份现有的/plugins/ch.acanda.eclipse.pmd.core_<blahblah#>.jar并将其替换为此临时jar。
  7. Exit Eclipse and Restart 退出Eclipse并重启

You can use your custom rule classes if you add your jar as an Eclipse plug-in fragment to your Eclipse installation. 如果将jar作为Eclipse插件片段添加到Eclipse安装中,则可以使用自定义规则类。 The host of the plug-in fragment must be ch.acanda.eclipse.pmd.core . 插件片段的主机必须是ch.acanda.eclipse.pmd.core

To convert the jar to a plug-in fragment you have to modify the file MANIFEST.MF file and add a few manifest headers. 要将jar转换为插件片段,您必须修改文件MANIFEST.MF文件并添加一些清单头。 The following is a complete manifest of an Eclipse plug-in fragment: 以下是Eclipse插件片段的完整清单:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Custom PMD Rules
Bundle-SymbolicName: org.example.customrules
Bundle-Version: 1.0.0
Fragment-Host: ch.acanda.eclipse.pmd.core;bundle-version="1.4.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

You can choose your own values for the headers Bundle-Name , Bundle-SymbolicName and Bundle-Version . 您可以为标题Bundle-NameBundle-SymbolicNameBundle-Version选择自己的值。 The value of the header Bundle-ManifestVersion must be 2 . 标头Bundle-ManifestVersion的值必须为2 Fragment-Host must contain the symbolic name of the host bundle ch.acanda.eclipse.pmd.core and optionally the version of eclipse-pmd you have installed. Fragment-Host必须包含主机包的符号名称ch.acanda.eclipse.pmd.core以及您已安装的eclipse-pmd的版本。 Bundle-RequiredExecutionEnvironment specifies the minimum execution environment the plug-in fragment requires. Bundle-RequiredExecutionEnvironment指定插件片段Bundle-RequiredExecutionEnvironment的最小执行环境。 Its value is JavaSE-1.7 if you compiled it with Java 7 or JavaSE-1.8 if you used Java 8. 如果使用Java 8编译它,则使用Java 7或JavaSE-1.8编译它的值为JavaSE-1.7

Once you changed the manifest of your jar you can copy it into the folder dropins of your eclipse installation. 更改jar的清单后,可以将其复制到eclipse安装的文件夹dropins中。 Your plug-in fragment is installed when you start Eclipse. 启动Eclipse时会安装插件片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM