如何在同一个项目中组合库代码和maven插件?

时间:2022-07-23 17:17:11

Can I make a single maven project that can be included as a dependency (to reference Java classes inside) and executed as a plugin?

我可以创建一个可以作为依赖项包含在内的maven项目(引用内部的Java类)并作为插件执行吗?

I'm working on a library to help with hosting GWT on a LAMP stack. For someone to use this, they need to extend some Java classes (so it must be a dependency) and they need to invoke a maven plugin (so it needs to be a plugin). The plugin code references the same Java classes, so if they are seperate projects, the plugin one must depend on the library one.

我正在开发一个库来帮助在LAMP堆栈上托管GWT。对于某人使用它,他们需要扩展一些Java类(因此它必须是依赖项)并且他们需要调用maven插件(因此它需要是一个插件)。插件代码引用相同的Java类,因此如果它们是单独的项目,则插件必须依赖于库1。

As is, I have the library as a normal maven project, and the plugin as a maven plugin that depends on the library. This means that to do a release, I have to release two different artifacts, and the dependent project must update both version numbers for both artifacts. It'd be nice to have a single project.

我将库作为普通的maven项目,并将插件作为依赖于库的maven插件。这意味着要进行发布,我必须发布两个不同的工件,并且依赖项目必须更新两个工件的两个版本号。拥有一个项目真是太好了。

3 个解决方案

#1


You'd be better of by doing the following

通过以下方式你会更好

  1. project for the jar, Foo:Foo.jar
  2. jar的项目,Foo:Foo.jar

  3. project that uses Foo:Foo.jar as a dependency that builds the plugin
  4. 使用Foo:Foo.jar作为构建插件的依赖项的项目

  5. Maven parent project that builds 1&2
  6. 构建1和2的Maven父项目

The directory structure would look like this

目录结构如下所示

\project\pom.xml
\project\foo\pom.xml
\project\foo\src\main\java\foo.java
\project\plugin\pom.xml
\project\plugin\src\main\resources
\project\plugin\src\main\java

From \project you can do a mvn clean package to build \project\foo\target\foo.jar and \project\plugin\target\plugin.jar

从\ project你可以做一个mvn clean包来构建\ project \ foo \ target \ foo.jar和\ project \ plugin \ target \ plugin.jar

Hope this helps.

希望这可以帮助。

#2


If you create a maven plugin it still has a artifactId/groupId/version. There's no reason it can't be references both in your section and in your section. On the other hand, if thats ugly, why not just make a library with the common code that both your main project and your maven plugin project depend on?

如果你创建一个maven插件,它仍然有一个artifactId / groupId / version。没有理由不能在您的部分和您的部分中引用它们。另一方面,如果这很难看,为什么不只是使用您的主项目和maven插件项目所依赖的公共代码创建一个库?

EDIT:

Sorry, wasn't clear on the second part. Look into composite maven projects, where there is a top level pom that defines a number of child modules. In this case, the maven plugin and the common library code could be separate children producing separate artifacts, but you only need one version number and one release command executed from the top level. I haven't done this but there are any number of open source projects that do. its often used as an idiom to put testing code into a single module that can be referenced by all the others, without having it go out in any distributable jar.

对不起,第二部分还不清楚。查看复合maven项目,其中有一个*pom定义了许多子模块。在这种情况下,maven插件和公共库代码可以是生成单独工件的独立子代,但是您只需要从*执行一个版本号和一个释放命令。我没有这样做,但有许多开源项目。它经常被用作将测试代码放入单个模块中的习惯用法,该模块可以被所有其他模块引用,而不会在任何可分发的jar中出现。

#3


The best practice is to not do what you're suggesting. Examples of this include PMD, BND, JUnit/TestNG, and so on - no serious projects seem to package the maven plugin with the library proper.

最好的做法是不做你的建议。这方面的例子包括PMD,BND,JUnit / TestNG等等 - 没有严肃的项目似乎将maven插件打包到适当的库中。

One way to get both alternatives is to use maven assemblies to have two seperate maven projects for each the library proper and the plugin and then a separate packaging as a jar containing the classes from both.

获得两种替代方法的一种方法是使用maven程序集为每个库本身和插件创建两个单独的maven项目,然后单独打包为包含两者类的jar。

#1


You'd be better of by doing the following

通过以下方式你会更好

  1. project for the jar, Foo:Foo.jar
  2. jar的项目,Foo:Foo.jar

  3. project that uses Foo:Foo.jar as a dependency that builds the plugin
  4. 使用Foo:Foo.jar作为构建插件的依赖项的项目

  5. Maven parent project that builds 1&2
  6. 构建1和2的Maven父项目

The directory structure would look like this

目录结构如下所示

\project\pom.xml
\project\foo\pom.xml
\project\foo\src\main\java\foo.java
\project\plugin\pom.xml
\project\plugin\src\main\resources
\project\plugin\src\main\java

From \project you can do a mvn clean package to build \project\foo\target\foo.jar and \project\plugin\target\plugin.jar

从\ project你可以做一个mvn clean包来构建\ project \ foo \ target \ foo.jar和\ project \ plugin \ target \ plugin.jar

Hope this helps.

希望这可以帮助。

#2


If you create a maven plugin it still has a artifactId/groupId/version. There's no reason it can't be references both in your section and in your section. On the other hand, if thats ugly, why not just make a library with the common code that both your main project and your maven plugin project depend on?

如果你创建一个maven插件,它仍然有一个artifactId / groupId / version。没有理由不能在您的部分和您的部分中引用它们。另一方面,如果这很难看,为什么不只是使用您的主项目和maven插件项目所依赖的公共代码创建一个库?

EDIT:

Sorry, wasn't clear on the second part. Look into composite maven projects, where there is a top level pom that defines a number of child modules. In this case, the maven plugin and the common library code could be separate children producing separate artifacts, but you only need one version number and one release command executed from the top level. I haven't done this but there are any number of open source projects that do. its often used as an idiom to put testing code into a single module that can be referenced by all the others, without having it go out in any distributable jar.

对不起,第二部分还不清楚。查看复合maven项目,其中有一个*pom定义了许多子模块。在这种情况下,maven插件和公共库代码可以是生成单独工件的独立子代,但是您只需要从*执行一个版本号和一个释放命令。我没有这样做,但有许多开源项目。它经常被用作将测试代码放入单个模块中的习惯用法,该模块可以被所有其他模块引用,而不会在任何可分发的jar中出现。

#3


The best practice is to not do what you're suggesting. Examples of this include PMD, BND, JUnit/TestNG, and so on - no serious projects seem to package the maven plugin with the library proper.

最好的做法是不做你的建议。这方面的例子包括PMD,BND,JUnit / TestNG等等 - 没有严肃的项目似乎将maven插件打包到适当的库中。

One way to get both alternatives is to use maven assemblies to have two seperate maven projects for each the library proper and the plugin and then a separate packaging as a jar containing the classes from both.

获得两种替代方法的一种方法是使用maven程序集为每个库本身和插件创建两个单独的maven项目,然后单独打包为包含两者类的jar。