使用 Maven 进行库开发

发布于 2024-09-25 07:43:17 字数 147 浏览 4 评论 0原文

这可能更多的是一个过程,而不是技术解决方案。我目前正在开发一个项目,该项目分为一个库和一个实施应用程序。这样做是在构建应用程序时调试库的一种非常有效的方法。我想对这两个项目使用 Maven,但我不想为每个微小的更改进行构建和部署。有没有办法让 Maven 几乎将项目引用为存储库?

This may be more of a process then technology solution. I am currently working on a project that is split into a library and an implementation application. Doing this has been a very effective way to debug the library as the application is built. I would like to use Maven for both projects but I do not want to do a build and deploy for each minor change. Is there a way for Maven to reference the project as the repository almost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-10-02 07:43:17

你说得对,这是一个流程问题。我将您的问题解释为希望避免将库的特定版本声明为实现中的依赖项。目的是独立更新任一组件,特别是您希望能够更新库而无需重建实现。

如果您进行更改,您显然需要重建并重新部署一些东西。但是,我可以看到两个步骤可以解决您的担忧。我将把你的组件称为“lib”和“app”。

  1. 创建一个空的“包装器”项目,依赖于(范围=运行时)lib 和应用程序。您将部署此包装器。当您更新 lib 或 app 时,只需更新包装器项目中该依赖项的版本即可。当您“构建”(编译是无操作)并测试包装器项目时,您将确认旧/新应用程序仍然可以与新/旧库一起使用,并且您可以部署更新。当然,如果您的测试失败,您将需要更新应用程序的 lib 依赖项以查看重建是否可以解决问题,并尝试查找哪个组件存在错误。所以我认为这只是一个折衷的解决方案。
  2. 更好的是还创建一个“lib-api”组件,它声明 lib 和 app 之间的接口。使 lib 和应用程序都依赖于 lib-api,但应用程序依赖于特定版本的 lib。您可能仍然需要如上所述的包装器项目,以方便部署(因为您需要实际部署 lib 和应用程序)。但只要您不需要影响库 api,您就可以确信对 lib 或应用程序的更改是相互保护的,并且不会(不能)需要重建。这也可能使追踪任何错误变得更加容易。如果您需要更新 lib-api,显然需要重建所有内容,但无论如何这都是真的,至少这样它会在项目结构中公开。

希望这对您有意义,并且我已经正确解释了您的问题。

You're right, this is a process question. I interpret your question as wanting to avoid declaring a specific version of the library as a dependency in the implementation. The intent is to update either component independently, in particular you want to be able to update the library without having to rebuild the implementation.

You will obviously need to rebuild and redeploy something if you make a change. However, I can see two steps that should address your concern. I'm going to call your components 'lib' and 'app'.

  1. Create an empty "wrapper" project, dependent (scope=runtime) on both lib and app. This wrapper is what you will deploy. When you update lib or app, you only need to update the version of that dependency in the wrapper project. When you "build" (compile is a no-op) and test the wrapper project, you'll confirm that the old/new app still works with the new/old lib, and you can deploy the update. Of course, if your tests fail you will need to update the app's lib dependency to see if a rebuild fixes the problem, and try to find which component has the bug. So this is only a halfway solution, in my opinion.
  2. Better is to also create a 'lib-api' component, which declares the interface between lib and app. Make lib and app both dependent on lib-api, but the app is not dependent on a specific version of lib. You may still want a wrapper project as above, to facilitate deployment (since you'll need to actually deploy both lib and app). But as long as you don't need to affect the library api, you can be confident that your changes to either lib or app are protected from each other and won't (can't) require a rebuild. This might make it easier to track down any bugs as well. If you need to update lib-api, you will obviously need to rebuild everything, but that would be true anyway and at least this way it's exposed in the project structure.

Hopefully this makes sense to you and I've correctly interpreted your question.

落花浅忆 2024-10-02 07:43:17

您可以拥有一个包含两个子项目的“super-pom”。一个项目将是您的图书馆,另一个项目将是您的应用程序。两者都可以单独开发。

您的应用程序将依赖于带有库的项目。

例如:

“super-pom”:将此pom.xml存放在上级目录中。你会在里面有这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>your.group</groupId>
    <artifactId>project</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>caap</name>
    <modules>
        <module>project-library</module>
        <module>project-app</module>
    </modules>
</project>

然后你的库 pom.xml 会有这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>project</artifactId>
        <groupId>your.group</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>=your.group</groupId>
    <artifactId>project-library</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>project-library</name>
</project>

你的应用程序会有这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>project</artifactId>
        <groupId>your.group</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>=your.group</groupId>
    <artifactId>project-application</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>project-application</name>
      <dependencies>
               <dependency>
           <artifactId>project-library</artifactId>
           <groupId>your.group</groupId>
                   <version>1.0-SNAPSHOT</version>
               </dependency>
      </dependencies>            
</project>

You can have a "super-pom" which has two sub-projects. One project would be your library and the other project would be your application. Both can be developed individually.

Your application would have a dependency on the project with the library.

For example:

"super-pom": store this pom.xml in the higher up directory. You would have something like this in it:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>your.group</groupId>
    <artifactId>project</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>caap</name>
    <modules>
        <module>project-library</module>
        <module>project-app</module>
    </modules>
</project>

Then your library pom.xml would have something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>project</artifactId>
        <groupId>your.group</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>=your.group</groupId>
    <artifactId>project-library</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>project-library</name>
</project>

And your app would have something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>project</artifactId>
        <groupId>your.group</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>=your.group</groupId>
    <artifactId>project-application</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>project-application</name>
      <dependencies>
               <dependency>
           <artifactId>project-library</artifactId>
           <groupId>your.group</groupId>
                   <version>1.0-SNAPSHOT</version>
               </dependency>
      </dependencies>            
</project>
献世佛 2024-10-02 07:43:17
mvn install

看起来像我想要的。只需从项目运行它就会将其添加到本地存储库,其他应用程序可以拾取它。

mvn install

Looks like what I want. Just running it from project will add it to the local repo and the other apps can pick it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文