【问题标题】:How do I build 2 jar files with different dependencies?如何构建 2 个具有不同依赖项的 jar 文件?
【发布时间】:2013-09-26 06:35:54
【问题描述】:

我有同一个第三方库的两个不同版本,需要部署到两个不同的系统。每个系统使用不同版本的库。

LibraryALibraryB 具有相同的 API 接口,我的代码目前使用 LibraryA 构建并部署到 SystemA。现在我需要使用 LibraryB 构建并部署到 SystemB 的第二个版本。

当然,我可以复制/粘贴我的代码并使用不同的库编译每个项目,但这似乎是维护的噩梦。这两个库可以同时安装和开发。目前我正在使用 Ant,但一切都在桌面上,任何可以提供帮助的东西都会被考虑。

【问题讨论】:

    标签: java ant build dependencies ivy


    【解决方案1】:

    你需要一个依赖管理器。 Apache ivy 将是我的首选工具。

    示例

    运行我的构建后:

    ├── build
    │   ├── ivy
    │   │   ├── com.myspotontheweb-demo-compile.html
    │   │   ├── com.myspotontheweb-demo-runtimeA.html
    │   │   ├── com.myspotontheweb-demo-runtimeB.html
    │   │   └── ivy-report.css
    │   ├── systemA
    │   │   ├── log4j-1.2.17.jar
    │   │   ├── slf4j-api-1.7.5.jar
    │   │   └── slf4j-log4j12-1.7.5.jar     <-- Version 1.7.5
    │   └── systemB
    │       ├── log4j-1.2.17.jar
    │       ├── slf4j-api-1.7.5.jar
    │       └── slf4j-log4j12-1.7.4.jar     <-- Version 1.7.4
    ├── build.xml
    └── ivy.xml
    

    注意:

    • systemAsystemB 目录包含同一个 jar 的两个不同版本。
    • 我已将构建配置为生成依赖项管理报告。对故障排除和文档很有用。

    ivy.xml

    Ivy 使用一个称为配置的概念来管理不同的依赖组。在这种情况下,我们为编译所需的 3rd 方 jar 创建一个配置,然后再创建两个来处理 SystemA 和 SystemB 依赖项:

    <ivy-module version="2.0">
        <info organisation="com.myspotontheweb" module="demo"/>
    
        <configurations>
            <conf name="compile"  description="Required to compile application"/>
            <conf name="runtimeA" description="System A runtime dependencies" extends="compile"/>
            <conf name="runtimeB" description="System A runtime dependencies" extends="compile"/>
        </configurations>
    
        <dependencies>
            <!-- compile dependencies -->
            <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="compile->default"/>
    
            <!-- runtimeA dependencies -->
            <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtimeA->default"/>
    
            <!-- runtimeB dependencies -->
            <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.4" conf="runtimeB->default"/>
    
        </dependencies>
    
    </ivy-module>
    

    build.xml

    <project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
    
       <property name="build.dir" location="build"/>
    
       <target name="bootstrap" description="Install ivy">
          <mkdir dir="${user.home}/.ant/lib"/>
          <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
       </target>
    
       <target name="resolve" description="Use ivy to resolve classpaths">
          <ivy:resolve/>
          <ivy:report todir='${build.dir}/ivy' graph='false' xml='false'/>
          <ivy:cachepath pathid="compile.path" conf="compile"/>
       </target>
    
       <target name="build" depends="resolve" description="Do something with each set of runtime jars">
          <ivy:retrieve pattern="${build.dir}/systemA/[artifact]-[revision](-[classifier]).[ext]" conf="runtimeA"/>
          <ivy:retrieve pattern="${build.dir}/systemB/[artifact]-[revision](-[classifier]).[ext]" conf="runtimeB"/>
       </target>
    
       <target name="clean" description="Cleanup build files">
          <delete dir="${build.dir}"/>
       </target>
    
       <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
          <ivy:cleancache/>
       </target>
    
    </project>
    

    注意事项:

    • retrieve 任务用于使用由 ivy 管理的文件集合填充所选目录。
    • bootstrap 目标将下载并安装 ivy 任务 jar
    • cachepathreport 任务是有用的附加任务
    • Ivy 会缓存下载的文件,因此最好包含 cleancache 任务(所有缓存都会变脏)

    【讨论】:

      【解决方案2】:

      我设法通过增强的 Ant 构建文件和 Eclipse 设置来实现这一点。这满足了我们在 Eclipse 中临时切换库、使用两个库进行编译/测试以及部署到不同环境的要求。

      最初在 LibraryA 上编译和测试的 Ant 构建脚本,我复制并粘贴了 LIbraryB 的另一部分。这允许项目同时使用 LibraryA 和 LibraryB 进行编译,并在这两个库上运行 JUnint 测试。

      启动脚本修改为 java -cp "Project.jar:$SYMLINK_LIB/*" project.main [空格分隔的参数]

      $SYMLINK_LIB 包含指向在两个不同环境之间不同的 jar 库的符号链接。

      Eclipse 配置

      1. 为 LibraryA 和 LibraryB 构建项目
      2. 对于这两个项目,右键单击项目 一种。单击“库”和“添加 JAR” 一世。添加此库所需的 jar 文件 湾。单击选项卡“订购和导出” ii.检查所有 jar 文件,以便导入此项目的任何项目都将使用它们。 C。点击“确定”
      3. 右键需要导入LibraryA和LibraryB的项目 一种。单击选项卡“项目”和“添加”以添加项目库 A(或库 B) 湾。如果此项目将由需要访问 LibraryA 或 LibraryB 的其他项目导入 - 单击“Order and Export”并选择 LibraryA 和 LibraryB。 C。点击“确定”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        • 2020-08-27
        • 1970-01-01
        • 2021-03-12
        相关资源
        最近更新 更多