Pergunta

I've seen different packages in source code such as com.website.package These packages are used across multiple applications, and I've been trying to accomplish something similar.

The only way I know of to achieve using the same packages in different projects is to copy each source file into the new project which would create the new packages. this probably isn't the preferred method, as it could possibly drag thousands of files into the project (see Java's library).

How would this be achieved?

TL;DR: How can I use a custom written package in multiple projects without copying many files? (aka Java's libraries).

Foi útil?

Solução

Take the class files and create a jar. Put the jar on the classpath of all the applications that need those classes.

How do you do that?

Inside an IDE, you can create multiple projects and put the shared java code in one project. Make the other projects depend on that project and you can share things within the IDE. That means you run inside the IDE and it will use the shared code.

For example, in Eclipse, you choose the project that will use the shared code/project. Right click on it in the package view or navigator view and choose "properties". Select the option to set the build path and there is a tab for selecting projects that this project depends on. Select the shared project and then it's code is callable from that project.

In this case, any change you make to the shared project will be immediately available, inside the IDE, to those projects that depend on it. Run them right then and the change will be in effect.

For running outside the IDE ...

If you are using Eclipse (or some other IDE) it will have an option for creating a jar. In Eclipse you create a java project and move the classes you want to be in the library/jar into this project. Then, once you have all the right classes, including those that others depend on, you will do some sort of build to create the jar.

Eclipse has an 'export' option on the 'file' menu. Use that and select to export as a "java" > "jar" and then select the project you just made.

You can also create a Maven project of type "jar" into which you put all those classes (as java files) and then building that project with Maven will create the jar. The "install" goal for Maven will deploy it to your local repository.

Any time your shared code is shared by way of a jar, you will have to rebuild the jar and copy it into the location(s) from which it is shared by other projects before changes inside the jar take effect.

Outras dicas

Maybe export the package to some place on the drive and create linked folders to it in the projects?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top