多个入口点(主类)位于JAR包顶层。

huangapple go评论113阅读模式
英文:

Multiple entry points (main classes) inside jar's top level

问题

以下是翻译好的内容:

值得一提的是,我正在使用Maven作为我的构建管理工具。我有一个JAR文件(我们称之为dep.jar),它将作为依赖项包含在最终项目(final.jar)中。
dep.jar有一个带有主方法的类。
我需要在final.jar的顶级目录中拥有多个入口点(带有主方法的类),这样我就可以根据需要使用不同的入口点,包括来自dep.jar的入口点。

我考虑过:

  1. 修改JAR文件中的META-INF/MANIFEST.MF文件。正如Oracle所述,不可能引用JAR的依赖项(BOOT-INF/lib目录)中的主类 -> https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html;
  2. Uber JAR - 不是一个选项,我依赖于Java代码库中的JAR目录结构;
  3. 使用特殊的类加载器,比如这个 http://www.jdotsoft.com/JarClassLoader.php。但这意味着要更改final.jar的主方法,由于项目限制,我无法这样做;
  4. 使用maven-dependency-plugin,但它可以解压内部JAR(dep.jar)并将类复制到Maven工作目录target,在打包阶段期间,这些类将被打包到BOOT-INF/classes目录中。同样,我无法从那里引用主类。如果我解压并将它们复制到与target不同的地方 - 复制的类将不会出现在我的final.jar中。

是否有任何其他插件或选项可以在JAR构建期间将final.jar依赖的JARdep.jar中的类添加到final.jar的顶级目录中?

编辑:
final.jar项目的结构如下:

final.jar
    |_______BOOT-INF
              |_______lib
              |         |_______dep.jar(包含我想要调用的主类)
              |_______classes
                         |__________dir(我想要在需要时通过CLI复制的目录)

英文:

It worth to mention that I am using maven as my build management tool. I have a jar (let's call it dep.jar) which will be included into the final project (final.jar) as dependency.
dep.jar has a class with main method.
I need to have several entry points (classes with main methods) within my final.jar's top level directory so I can use entry point depending on my need. Including one from dep.jar.

I considered:

  1. Changing META-INF/MANIFEST.MF file within jar. As Oracle stated that is not possible to reference main classes inside jar's dependencies (BOOT-INF/lib directory) -> https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html;
  2. Uber jar - not an option, I am dependent on jar directory structure inside Java code base
  3. Using special class loader like this one http://www.jdotsoft.com/JarClassLoader.php. But it implies changing final.jar's main method which I cannot do due to project restrictions.
  4. Using maven-dependency-plugin but it can unpack inner jar (dep.jar) and copy classes to maven working directory target which during packaging phase will be packed to BOOT-INF/classes directory. Again, I cannot reference main classes from there. If I unpack and copy them somewhere different than target - copied classes will not appear in my final.jar

Is there any other plugin or option how to add classes from final.jar dependant jar dep.jar during JAR build to final.jar's top level?

EDIT:
final.jar project looks like this:

final.jar
    |_______BOOT-INF
              |_______lib
              |         |_______dep.jar (contains main class I want to invoke)
              |_______classes
                         |__________dir (directory I want to copy on demand with help of CLI)

答案1

得分: 0

我在这里找到了一个解决方案,链接为:https://stackoverflow.com/questions/31076911/spring-boot-how-to-specify-an-alternate-start-class-multiple-entry-points。最终使用了在启动jar包时添加-Dloader.main属性的方法。
命令行如下所示:java -jar -Dloader.main=<main_class> ./final.jar

英文:

I found a solution here https://stackoverflow.com/questions/31076911/spring-boot-how-to-specify-an-alternate-start-class-multiple-entry-points. Ended up using -Dloader.main property when launching jar.
Command line looks like these: java -jar -Dloader.main=&lt;main_class&gt; ./final.jar

huangapple
  • 本文由 发表于 2020年4月3日 23:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61015646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定