[英]Java: Include external jars while exporting java project (not as a JAR) in eclipse
Now I have searched for sometime for my question and I haven't found my answer yet. 现在,我已经在某个时间搜索了我的问题,但尚未找到答案。
I have a java eclipse project with some external Jars like SWT jars. 我有一个Java eclipse项目,其中包含一些外部Jar(例如SWT jar)。 Now this project is a homework assignment project which I have been working on for some time.
现在,这个项目是我已经从事一段时间的作业分配项目。 I need to turn in the assignment and the professor will review the code and he will also execute it locally.
我需要交作业,教授将审查代码,他还将在本地执行代码。
So how do I export the project by keeping all the dependencies intact. 因此,如何通过保持所有依赖关系不变来导出项目。 I of course cannot submit a JAR file.
我当然不能提交JAR文件。 I have tried exporting the project as "File System".
我尝试将项目导出为“文件系统”。 However it is not preserving the external dependencies.
但是,它不保留外部依赖关系。
Is there a way to do this ? 有没有办法做到这一点 ?
Thanks. 谢谢。
If you don't use management dependency tools such as Maven, you have not the choice : you have to provide all jars needed to make your project compile. 如果您不使用诸如Maven之类的管理依赖工具,那么您就别无选择:您必须提供编译项目所需的所有jar。
In this case, if you teacher has ANT, an ant build would be a clean way to organize the dependencies. 在这种情况下,如果您的老师有ANT,则使用ant构建将是一种组织依赖关系的干净方法。
Otherwise, the manual solution would be to add all external dependencies in an folder of your project. 否则,手动解决方案是将所有外部依赖项添加到项目的文件夹中。 For example :
libs
. 例如:
libs
。 In this libs
folder, add all external jars. 在此
libs
文件夹中,添加所有外部jar。 And then zip/tar the project. 然后压缩/压缩项目。 The professor has just need to configure the project JDK and it should be ok.
教授只需要配置项目JDK,就可以了。
There is a way, with Maven, to download the required dependencies specified in the POM. 使用Maven,有一种方法可以下载POM中指定的必需依赖项。 This will only work if the dependencies you require are on some Maven repository (like central) or if they are installed to your local repository.
仅当您需要的依赖项位于某些Maven存储库(例如中央存储库)或将其安装到本地存储库中时,此方法才起作用。
Just add a plugin element configuring the maven-dependency-plugin to your POM. 只需将一个配置maven-dependency-plugin的插件元素添加到您的POM中即可。 This will copy all dependencies to a certain folder during the compile phase.
这将在编译阶段将所有依赖项复制到某个文件夹。 In this case: target/lib.
在这种情况下:target / lib。
<project>
...
<build>
...
<plugins>
...
<!-- For copying dependencies into target folder -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
If you were making a runnable JAR, you could also configure the maven-jar-plugin to add the dependencies copied to target/lib to the Class-Path element in the manifest and use a specified main class. 如果要创建可运行的JAR,还可以配置maven-jar-plugin将复制到target / lib的依赖项添加到清单中的Class-Path元素,并使用指定的主类。
<!-- For including dependencies in target folder in JAR manfiest Class-Path -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If using Maven isn't possible, download the JARs and put them in a lib folder with your project. 如果无法使用Maven,请下载JAR,然后将其放在项目的lib文件夹中。 An IDE will be able to pick those up if you tell it where to look.
如果您告诉IDE在哪里查看,IDE将能够接管这些对象。 Or just use the
javac
and java
commands with the -cp
flag to specify where the dependencies are. 或者只使用带有
-cp
标志的javac
和java
命令来指定依赖项在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.