[英]Add JAR-File to web-inf/lib folder without including it in pom.xml
I am writing an application server using JBoss. 我正在使用JBoss编写应用程序服务器。 The application gets compiled by Maven and packaged as WAR. 该应用程序由Maven编译并打包为WAR。 Now I want to externalize some classes. 现在,我想外部化一些类。 I create another maven project, which is packaged as JAR. 我创建另一个打包为JAR的Maven项目。
The JAR has the correct directory tree, I believe. 我相信JAR具有正确的目录树。 It contains a META-INF
directory and the package structure as directories, in which the class files reside. 它包含一个META-INF
目录以及作为目录的包结构,类文件位于其中。
I read on the web that I can add this JAR into the app server by placing it into src\\main\\webapp\\WEB-INF\\lib
without further mentioning it in the pom.xml of the appserver (at some point these JARs shall be plugins, so adding a dependency in the pom.xml is not an option). 我在网络上阅读到,可以将此JAR放入src\\main\\webapp\\WEB-INF\\lib
并将其添加到应用服务器中,而无需在应用服务器的pom.xml中进一步提及(有时这些JAR应该是插件,因此不能在pom.xml中添加依赖项)。
When I compile the server, the JAR gets correctly copied into the WAR under WEB-INF\\lib
, along with other external dependencies. 当我编译服务器时,将JAR与其他外部依赖项一起正确复制到WEB-INF\\lib
下的WAR中。
However, when I try to use the class in the appserver, (writing an import
statement of such a class is enough), maven will complain that it can't find the class. 但是,当我尝试在应用服务器中使用该类时(编写此类的import
语句就足够了),maven会抱怨找不到该类。
I tried to find information on the web, but all I could find was that this is the way it's to be done. 我试图在Web上查找信息,但我能找到的只是这是要完成的方式。
Does anyone know where I could further look for clues what's going wrong? 有谁知道我可以在哪里进一步寻找线索哪里出了问题?
Putting the jar file into src\\main\\webapp\\WEB-INF\\lib
is cumbersome, and plainly wrong. 将jar文件放入src\\main\\webapp\\WEB-INF\\lib
很麻烦,这显然是错误的。
The reason that the code does not compile is that maven compiler plugin uses defined source paths and (provided and compile scoped) dependencies. 代码无法编译的原因是maven编译器插件使用了已定义的源路径和(提供和编译范围内的)依赖项。
You could change this, but the result would be hard to understand. 您可以更改此设置,但结果将难以理解。 Also, you would have to update your jar file (which, BTW has no place in your source code) by hand. 另外,您还必须手动更新jar文件(BTW在源代码中没有位置)。
Thats what dependency management is for. 那就是依赖管理的目的。
Don't work against your tool. 不要反对您的工具。 If your project needs classes from this jar, it is a dependency. 如果您的项目需要此jar中的类,则为依赖项。 So declare it as such. 因此就这样声明。
If you're talking about "plugins" then you shouldn't be importing the classes directly in your code anyway. 如果您在谈论“插件”,那么无论如何您都不应该直接在代码中导入类。 A common approach would be to define another module with interfaces that your plugins will implement, then have both your main webapp and the plugin projects depend on this interface module. 一种常见的方法是使用您的插件将要实现的接口定义另一个模块,然后让您的主webapp和插件项目都依赖于此接口模块。 In the main app you load plugin classes by reflection and cast them to the appropriate interface(s). 在主应用程序中,您通过反射加载插件类,并将其转换为适当的接口。 That way you don't depend directly on the plugins' concrete classes when compiling the host application. 这样,您在编译主机应用程序时就不会直接依赖于插件的具体类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.