简体   繁体   English

Java-模块化库项目-如何?

[英]Java - modular library project - how to?

I am supposed to deliver a SDK in Java for the company I work for. 我应该为我工作的公司提供Java SDK。 I have a few years of Java EE experience but not so much when it comes to develop API and SDK. 我有几年的Java EE经验,但是在开发API和SDK方面经验不多。

The problem here, is that the SDK is already available in .NET C# and working just fine. 这里的问题是,SDK已经可以在.NET C#中使用并且可以正常工作了。 I made it myself. 我自己做的。 Switching to Java is a nightmare. 切换到Java是一场噩梦。 I tried several solutions. 我尝试了几种解决方案。 All failed, some were inconclusive due to the fact I could not progress. 都失败了,由于我无法前进,所以有些不确定。

As the title says, I a need to develop a Java Library that has modules. 如标题所示,我需要开发一个具有模块的Java库。 Internal modules. 内部模块。 Modules the world out there is NOT supposed to see / use / modify. 世界各地的模块不应该看到/使用/修改。

In C#, it's easy as pie : create your modules respective namespace, make their classes and methods privates, expose one or more wrappers (bridges) to the entire assembly (project) with internal so that the "main module", through it's own public wrapper accessible by the world, can use the tools provided within these internals modules. 在C#中,这很容易做到:创建模块各自的命名空间,将其类和方法设为私有,使用内部将一个或多个包装器(桥)公开给整个程序集(项目),以便“主模块”通过自己的公共全世界都可以使用的包装器,可以使用这些内部模块中提供的工具。

The keyword, here, is INTERNAL. 关键字在这里是INTERNAL。 I think it's pretty easy to understand. 我认为这很容易理解。 So let's take an example. 因此,让我们举个例子。 Let's say the SDK is consisting of 4 modules. 假设SDK由4个模块组成。

  1. The 1st module, is the main module, the one that is public and exposed to the whole world. 第一个模块是主要模块,是公开的并且向全世界公开的模块。 In other words, the unique entry point of the SDK. 换句话说,SDK的唯一入口。 It's like a master of its own universe. 就像是自己宇宙的主人。 It can use the internal modules at will, but will never show them to the world. 它可以随意使用内部模块,但永远不会向世界展示它们。 Never. 决不。
  2. The 2nd module is network-related. 第二个模块与网络有关。 As in, it deals with network to manage connexions to remote services, read and write data from and to a stream. 与之类似,它处理网络以管理与远程服务的连接,从流中读取和写入数据。 It offers its own little wrapper so that the main module does not need to use the 2nd module internal tools. 它提供了自己的小包装器,因此主模块不需要使用第二个模块的内部工具。 Like a universe inside a universe. 就像宇宙内部的宇宙一样。
  3. The 3rd module is a data processing module. 第三模块是数据处理模块。 It receives packets (hand-made by the 2nd module) so that it can be processed and relevant information dealt with. 它接收数据包(由第二个模块手工制作),以便可以对其进行处理并处理相关信息。

So here we are. 所以我们到了。 How can I do that in Java using NetBeans ? 如何使用NetBeans在Java中做到这一点?

I tried Maven with POM Project, and Netbeans modules. 我用POM项目和Netbeans模块尝试了Maven。 A nightmare. 一个噩梦。

I tried creating multiple libraries (one per module), tweaked the Main Module library to include its (modules) dependancies but it does not work. 我尝试创建多个库(每个模块一个),调整了主模块库以包括其(模块)相关性,但是它不起作用。 In a standard Java EE console application, as soon as I attempt to instanciate my SDK Manager (ergo the main wrapper from the main module), it fails because Class Not Found exception : could not find classes related to the internal sub-modules. 在标准的Java EE控制台应用程序中,一旦我尝试实例化我的SDK Manager(从主模块中遍历主包装器),它就会失败,因为Class Not Found异常:找不到与内部子模块相关的类。 If I add all modules respective Jar into this Java Console app, it can access all wrappers. 如果我将所有模块(分别是Jar)添加到此Java Console应用程序中,则它可以访问所有包装器。 Where is the fun in that ? 那里的乐趣在哪里?

Thanks for the help ! 谢谢您的帮助 !

Project Jigsaw will eventually give you what you want when java 9 comes out. 当Java 9发布时,Project Jigsaw最终将为您提供所需的内容。

See this article about how jigsaw works, in particular, 请参阅本文 ,了解拼图的工作原理,特别是,

An exports clause in a module declaration makes the public types in the package it names available to other modules, so we can with Jigsaw defines boundaries, and not all public types could be used from other modules, we must explicitly specify which types are visible. 模块声明中的export子句使它命名的包中的公共类型可用于其他模块,因此我们可以使用Jigsaw定义边界,并且并非所有公共类型都可用于其他模块,我们必须明确指定哪些类型可见。

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

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