简体   繁体   English

如何在Eclipse中将项目的jar版本添加到项目中?

[英]How to add a jar build by a project to the project in eclipse?

I have a project which as part of the build process creates an XMLBeans jar file (stbSchemas.jar) which I want to include and reference in this project. 我有一个项目,该项目在构建过程中会创建一个XMLBeans jar文件(stbSchemas.jar),我想在该项目中包括并引用该文件。

Is this the best way to go about this (Single project) or should I have a child project which is built from the parent project? 这是解决此问题的最佳方法(单个项目)还是应该有一个由父项目构建的子项目?

I am building this using Maven2 inside Eclipse. 我正在Eclipse中使用Maven2构建它。 Is there a better way to do this so that I can maintain the integrity of the projects and stability of the builds. 有没有更好的方法可以做到这一点,以便我可以维护项目的完整性和构建的稳定性。

Hmm. 嗯。 If I understand correctly, you're saying that the you need to reference the stbSchemas.jar in your project in order to build some code that DOESN'T go into the stbSchemas.jar file. 如果我理解正确的话,就是说您需要在项目中引用stbSchemas.jar以便构建一些不会放入stbSchemas.jar文件中的代码。 If that's a correct assumption, then I think you should probably do a little bit of refactoring so that you have something like this: 如果这是一个正确的假设,那么我认为您可能应该进行一些重构,以便您具有以下内容:

1) a Maven parent pom file at the top level of the project. 1)项目顶层的Maven父pom文件。 That parent pom has a modules section that would reference the 2 modules from step 2. EG: 该父pom的模块部分将引用步骤2中的2个模块。EG:

<modules>
  <module>stbSchemas</module>
  <module>core</module>
</modules>

2) Split the code into clean modules. 2)将代码拆分为干净的模块。 stbSchemas would be the first, your other code would be a 2nd module. stbSchemas将是第一个,其他代码将是第二个模块。 Each of those modules gets its own pom.xml file that would reference the parent pom file like this: 每个模块都有自己的pom.xml文件,该文件将引用父pom文件,如下所示:

<parent>
       <groupId>parent.group</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>

The second module's pom.xml file would also need a dependency section for stbSchemas.jar, like this: 第二个模块的pom.xml文件还需要stbSchemas.jar的依赖项部分,如下所示:

<dependency>
  <groupId>my.group</groupId>
  <artifactId>stbSchemas</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

Then, when you build your second module, it'll pick up stbSchemas.jar. 然后,当您构建第二个模块时,它将选择stbSchemas.jar。

3) Once you've got that (or something similar) setup in Maven, you can use the maven eclipse plugin (mvn eclipse:eclipse) to generate a correct eclipse classpath for the whole project. 3)一旦在Maven中进行了设置(或类似的设置),就可以使用maven eclipse插件(mvn eclipse:eclipse)为整个项目生成正确的Eclipse类路径。 You can then refresh the project in Eclipse, and that will put stbSchemas.jar on the classpath for you. 然后,您可以在Eclipse中刷新项目,这会将stbSchemas.jar放在您的类路径中。

My suggestion is that, when you build the jar using Maven, lets have one folder say "runtime" in the project where you want to use this jar. 我的建议是,当您使用Maven构建jar时,在要使用此jar的项目中让一个文件夹说“运行时”。 And through maven only copy the jar to runtime of the project as soon as the jar is created so whenever you will have a new build, you will have the latest jar in the runtime folder. 并且通过maven仅在创建jar时将jar复制到项目的运行时,因此只要您有新的构建,就将在runtime文件夹中拥有最新的jar。 And now make your project to refer to the jar from the runtime folder. 现在,使您的项目引用运行时文件夹中的jar。 by adding it in its build path. 通过将其添加到其构建路径中。 Hope this helps 希望这可以帮助

编写脚本以将jar复制到所需的文件夹,并在项目的.settings文件夹中编辑xml文件,以通过相同的脚本将jar包含为参考,或在运行时将其添加到其buildpath中。

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

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