如何在我的jar中包含外部类

时间:2022-03-15 10:35:49

I have a problem with a service I am trying to write. I am trying to create a service that runs in the background on a windows system but uses java. I have seen several ways of doing this, but decided on one method that seemed to meet my requirements. The service will check a database for items it needs to work on. When it finds an item in the DB that it needs to do it will run some system commands to take care of them.

我有一个问题,我正在尝试写一个服务。我正在尝试创建一个在Windows系统后台运行但使用java的服务。我已经看到了几种方法,但决定了一种似乎符合我要求的方法。该服务将检查数据库中是否需要处理的项目。当它在DB中找到它需要执行的项时,它将运行一些系统命令来处理它们。

I found a way to use the tomcat7.exe file to run a jar as a service and that worked pretty well for basic stuff. Anything I write and compile into my jar file "myService.jar" we'll can call it goes well enough. The problem is that we already have several classes written for accessing the DB and running commands that are precompiled in a library of classes called BGLib-1.0.jar.

我找到了一种方法来使用tomcat7.exe文件来运行jar作为服务,这对于基本的东西非常有用。我编写并编译到我的jar文件“myService.jar”的任何内容我们都可以调用它。问题是我们已经编写了几个类来访问DB并运行在名为BGLib-1.0.jar的类库中预编译的命令。

I have used this library in writing several jenkins plugins and had no problems calling functions from it. They all work fine when I create an hpi file and deploy it in Jenkins. There the compiler (Eclipse using Maven) packages the BGLib jar in with the plugin jar and Jenkins figures out how to get them to see one another.

我已经使用这个库编写了几个jenkins插件,并且从它调用函数没有任何问题。当我创建一个hpi文件并将其部署在Jenkins中时,它们都可以正常工作。在那里,编译器(Eclipse使用Maven)将BGLib jar打包到插件jar中,Jenkins想出如何让他们看到彼此。

When I build my service jar, however, it doesn't work when I deploy it.

但是,当我构建我的服务jar时,它在部署时不起作用。

I run a command like this to install the Tomcat exe renames to myservice.exe:

我运行这样的命令来安装Tomcat exe重命名到myservice.exe:

d:\myService\bin>myService.exe //IS//myService --Install=D:\myService\bin\myService.exe --Description="run some commands
Java Service" --Jvm=auto --Classpath=D:\myService\jar\myService.jar;D:\myService\jar\BGLib-1.0.jar --StartMode=jvm --
StartClass=com.myCompany.myService.myService --StartMethod=windowsService --StartParams=start --StopMode=jvm --StopClass
=com.myCompany.myService.myService --StopMethod=windowsService --StopParams=stop --LogPath=D:\myService\logs --StdOutpu
t=auto --StdError=auto

When I deploy this with code solely within the myService.jar the service behaves as expected, but when I try to call functions within the BGLib-1.0.jar I get nothing. The jvm appears to crash or become unresponsive. Debugging is a little tricky but it looks like I am getting class not found errors.

当我在myService.jar中使用代码部署它时,服务按预期运行,但是当我尝试在BGLib-1.0.jar中调用函数时,我什么也得不到。 jvm似乎崩溃或没有响应。调试有点棘手,但看起来我得到的类找不到错误。

I tried adding the entry below in the POM file to see if changing the classpath entry in the manifest would help, but it didn't change the manifest. I am still kind of clueless ass to how the manifest file works. Any documentation on that would be cool. I have been to Maven's site and it doesn't seem to have comprehensive documentation on the tags available. Is there something I need to change in the manifest to get my jar to see external classes? Or is there something I can add that will get Maven to compile the classes from that jar in with my jar?

我尝试在POM文件中添加以下条目,以查看更改清单中的类路径条目是否有帮助,但它没有更改清单。我仍然对清单文件的工作方式毫无头绪。关于这方面的任何文件都很酷。我去过Maven的网站,似乎没有关于可用标签的全面文档。我需要在清单中更改一些东西才能让我的jar看到外部类吗?或者我可以添加一些东西,让Maven用我的jar从那个罐子里编译类?

thanks in advance.

提前致谢。

<configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.myCompany.myService.myService</mainClass>
              <customClasspathLayout>BGLib-1.0.jar</customClasspathLayout>
            </manifest>
          </archive>
        </configuration>

3 个解决方案

#1


0  

To answer mainly the question of the title, you can the shade plugin to include dependencies into your final jar. You can even even relocate the class files (e.g. change package name) within the final jar so that the included classes don't conflict with different versions of the shaded dependency on the classpath. Not sure if this is the best solution for your particular problem though.

要主要回答标题的问题,你可以使用shade插件将依赖项包含到最终的jar中。您甚至可以在最终的jar中重新定位类文件(例如更改包名称),以便包含的类不会与类路径上的着色依赖项的不同版本冲突。不确定这是否是针对您的特定问题的最佳解决方案。

#2


0  

You can use the maven-dependency-plugin unpack-dependencies goal to include the contents of a dependency in the resulting artifact.

您可以使用maven-dependency-plugin unpack-dependencies目标在生成的工件中包含依赖项的内容。

An example of how to do this would be:

如何执行此操作的示例将是:

        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>${project.artifactId}-fetch-deps</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <stripVersion>true</stripVersion>
                        <excludeTransitive>true</excludeTransitive>
                        <includeArtifactIds>protobuf-java</includeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>

This will expand the protobuf-java dependency (flatten it) and include the contents in the resulting artifact generated by your build.

这将扩展protobuf-java依赖项(展平它)并将内容包含在构建生成的结果工件中。

#3


0  

Looks to me you actually want to use the appassembler-maven-plugin, otherwise I'd go for the maven-shade-plugin.

在我看来你实际上想要使用appassembler-maven-plugin,否则我会选择maven-shade-plugin。

#1


0  

To answer mainly the question of the title, you can the shade plugin to include dependencies into your final jar. You can even even relocate the class files (e.g. change package name) within the final jar so that the included classes don't conflict with different versions of the shaded dependency on the classpath. Not sure if this is the best solution for your particular problem though.

要主要回答标题的问题,你可以使用shade插件将依赖项包含到最终的jar中。您甚至可以在最终的jar中重新定位类文件(例如更改包名称),以便包含的类不会与类路径上的着色依赖项的不同版本冲突。不确定这是否是针对您的特定问题的最佳解决方案。

#2


0  

You can use the maven-dependency-plugin unpack-dependencies goal to include the contents of a dependency in the resulting artifact.

您可以使用maven-dependency-plugin unpack-dependencies目标在生成的工件中包含依赖项的内容。

An example of how to do this would be:

如何执行此操作的示例将是:

        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>${project.artifactId}-fetch-deps</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <stripVersion>true</stripVersion>
                        <excludeTransitive>true</excludeTransitive>
                        <includeArtifactIds>protobuf-java</includeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>

This will expand the protobuf-java dependency (flatten it) and include the contents in the resulting artifact generated by your build.

这将扩展protobuf-java依赖项(展平它)并将内容包含在构建生成的结果工件中。

#3


0  

Looks to me you actually want to use the appassembler-maven-plugin, otherwise I'd go for the maven-shade-plugin.

在我看来你实际上想要使用appassembler-maven-plugin,否则我会选择maven-shade-plugin。