Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Java Include Packages in Netbeans

Once you have built your own packages, or if you want to use somebody elses, you will need to include them in other projects. Here is how one does that inside the Netbeans IDE.

Right-click on the libraries folder in the projects pane, and select add JAR/Folder.

Navigate to the jar you want to add to your project and click ok.

Add an import statement for the package you just included so that you can now use its classes. For example, my package "programster.core" had the Core class which I use as shown below:

Java - Create Packages

Creating packages is a great way to share and reuse code. Once you have developed/found lots of packages, you will find that you will be able to build complex applications rapidly by just glueing them together. You will need to write less code, thus have less code to maintain and debug.

    Create a set of useful classes that you want to turn into a package. I can't really help you with this one, but for this example, I am going to use the files shown below:
    Compile the source files using javac into class files.
    javac /path/to/source/*
    We need to know your "vendor" name. If you have a personal website, this would be your website's address in reverse, such as com.blogspot.programster, however, since I am not sharing this publicly, I am going to just use "programster".
    Think of a name for your package. E.g. for my first package, I am going to call it "core" because it forms my "core" libraries that I use in all projects.
    The vendor name and package name need to be all lowercase, not have any hyphens, but can have underscores.
    Place your
    *.class
    files, inside a folder that is named after the package, and place that folder within a folder named after the vendor. If your vendor name has
    .
    characters, such as for the reversed domain, create folders for each part of the name, not including the
    .
    . For example, my folder path (if I was using this domain for my vendor name) would be:
    com/blogspot/programster/core/[java source files here]
    Navigate to the folder that contains the top folder of the path you just created. and run the following command:
    jar -cf [package-name].jar [top folder name]
    For example:
    java -cf core.jar com
    Now you should have a file called [package name].jar in the folder that you just ran the command. This is your finished package that you can share with others and include in projects.

Maven Build Lifecycle

The Maven "buld lifecycle" is a defined process for building and distributing a particular project (what they call artifact). Each step in the build lifecycle is referred to as a "phase". Whenever a phase is specified after the call to

maven
, e.g.
mvn compile
, all phases previous to, and including, the specified phase are executed. The default phase table is below, in order from or execution:



validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Sources