英文:
Multiple entry points (main classes) inside jar's top level
问题
以下是翻译好的内容:
值得一提的是,我正在使用Maven作为我的构建管理工具。我有一个JAR文件(我们称之为dep.jar
),它将作为依赖项包含在最终项目(final.jar
)中。
dep.jar
有一个带有主方法的类。
我需要在final.jar
的顶级目录中拥有多个入口点(带有主方法的类),这样我就可以根据需要使用不同的入口点,包括来自dep.jar
的入口点。
我考虑过:
- 修改JAR文件中的
META-INF/MANIFEST.MF
文件。正如Oracle所述,不可能引用JAR的依赖项(BOOT-INF/lib
目录)中的主类 -> https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html; - Uber JAR - 不是一个选项,我依赖于Java代码库中的JAR目录结构;
- 使用特殊的类加载器,比如这个 http://www.jdotsoft.com/JarClassLoader.php。但这意味着要更改
final.jar
的主方法,由于项目限制,我无法这样做; - 使用
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:
- 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; - Uber jar - not an option, I am dependent on jar directory structure inside Java code base
- 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. - Using
maven-dependency-plugin
but it can unpack inner jar (dep.jar
) and copy classes to maven working directorytarget
which during packaging phase will be packed toBOOT-INF/classes
directory. Again, I cannot reference main classes from there. If I unpack and copy them somewhere different thantarget
- copied classes will not appear in myfinal.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=<main_class> ./final.jar
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论