简体   繁体   English

如何构造JVM项目以隔离专有代码?

[英]How to structure a JVM project to isolate proprietary code?

I have a scala project managed by SBT, which includes a number of classes that are adapters that link to proprietary code. 我有一个由SBT管理的scala项目,其中包括许多类,这些类是链接到专有代码的适配器。 These have commercial licenses that preclude distribution of linked libraries, as well as the adapter code that I've written myself. 它们具有禁止分发链接库的商业许可,以及我自己编写的适配器代码。

I want to isolate these adapters from the rest of my code so that I could in theory distribute my project as open-source. 我想将这些适配器与我的其余代码隔离开,以便从理论上讲我可以将我的项目作为开源分发。 I need to ensure that each adapter 我需要确保每个适配器

  • Is version-controlled separately. 分别受版本控制。
  • Can be selectively switched on/off during compilation & JAR creation (kind of like --with-xxx when running ./configure in a C project) 可以在编译和JAR创建期间有选择地打开/关闭(在C项目中运行./configure时类似于--with-xxx

What should the directory structure of something like this look like? 这样的目录结构应该是什么样的? What about the SBT project(s)? SBT项目如何? I'd like to adhere to the usual Gradle directory structure if possible. 如果可能的话,我想坚持通常的Gradle目录结构。

The problem to consider is that each adapter depends on the core project (ie they inherit an Adapter interface defined in the main project), and in turn the core project depends on individual adapters. 要考虑的问题是每个适配器都依赖于核心项目(即,它们继承了主项目中定义的Adapter接口),而核心项目又依赖于各个适配器。 So I'm not sure how to set this up to avoid circular dependencies, while isolating the core code from the adapter code. 因此,我不确定在设置核心代码与适配器代码的同时如何设置以避免循环依赖。

It can be achieved this way: 可以通过以下方式实现:

  1. Extract the adapter interface from the 'core' module into a new module 'adpater-api'; 将适配器接口从“核心”模块中提取到新模块“ adpater-api”中;
  2. Declare it as a dependency for the 'core' module and all the adapter implementation modules; 将其声明为“核心”模块和所有适配器实现模块的依赖项;
  3. Declare 'core' as a dependency of the adapter implementation modules. 将“核心”声明为适配器实现模块的依赖项。
  4. Each module can be versioned separately. 每个模块可以分别进行版本控制。
  5. And adapter implementation modules are packaged separately. 适配器实现模块是单独包装的。

Project layout: 项目布局:

root
├── adapter-a
│   └── src
│       └── main
│           ├── resources
│           └── scala
├── adapter-api
│   └── src
│       └── main
│           ├── resources
│           └── scala
├── adapter-b
│   └── src
│       └── main
│           ├── resources
│           └── scala
└── core
    └── src
        └── main
            ├── resources
            └── scala

build.sbt: build.sbt:

lazy val root = project
  .in(file("."))
  .aggregate(adapterApi, core, adapterA, adapterB)

lazy val adapterApi = project
  .in(file("adapter-api"))
  .settings(
    version := "1.0"
  )

lazy val core = project
  .in(file("core"))
  .dependsOn(adapterApi)
  .settings(
    version := "1.5"
  )

lazy val adapterA = project
  .in(file("adapter-a"))
  .dependsOn(adapterApi, core)
  .settings(
    version := "1.2"
  )

lazy val adapterB = project
  .in(file("adapter-b"))
  .dependsOn(adapterApi, core)
  .settings(
    version := "1.0"
  )

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

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