Netbeans的编译和打包ant脚本----英文版

1. How change build options in netbeans

I have a project in netbeans in which I have a java scanner which has been generated using jflex. So when I usually build this using a make file I use the -Xlint option, that is

javac -Xlint javaScanner.java

But now when I am creating the jar file using the clean and build option it is showing the following error.

init:
deps-clean:
Updating property file: /home/mridula/testPrjct/sDvait-LCS/build/built-clean.properties
Deleting directory /home/mridula/testPrjct/sDvait-LCS/build
clean:
init:
deps-jar:
Created dir: /home/mridula/testPrjct/sDvait-LCS/build
Updating property file: /home/mridula/testPrjct/sDvait-LCS/build/built-jar.properties
Created dir: /home/mridula/testPrjct/sDvait-LCS/build/classes
Created dir: /home/mridula/testPrjct/sDvait-LCS/build/empty
Created dir: /home/mridula/testPrjct/sDvait-LCS/build/generated-sources/ap-source-output
Compiling 35 source files to /home/mridula/testPrjct/sDvait-LCS/build/classes
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 1 file to /home/mridula/testPrjct/sDvait-LCS/build/classes
compile:
Created dir: /home/mridula/testPrjct/sDvait-LCS/dist
Copying 1 file to /home/mridula/testPrjct/sDvait-LCS/build
Copy libraries to /home/mridula/testPrjct/sDvait-LCS/dist/lib.
Building jar: /home/mridula/testPrjct/sDvait-LCS/dist/sDvait-LCS.jar
To run this application from the command line without Ant, try:
java -jar "/home/mridula/testPrjct/sDvait-LCS/dist/sDvait-LCS.jar"
jar:
BUILD SUCCESSFUL (total time: 2 seconds)

If I could know how the file is being built. I could just add the -Xlint option so that the jar file could be created without a problem.

Answer

Netbeans uses ant to build java projects. There is a build.xml file in your project dir that handles this process.

But you have another option:

Right click your project node, choose "Properties" (a dialog box pops up). On the left choose "Compiling" and add your options in the field "Additional compiler options"

ME

It seems the answer doesn't work....

 

2. How to include jars in lib into project jar file in Netbeans?

I'm using some apache HttpClient jars, so I put them in my project's lib directory, when Netbeans compiles the project, it copies the lib and generated a project jar into dist directory, but when I run my project with webstart, I need to copy the project jar and lib into tomcat's ROOT dir, I wonder if Netbeans can generate a project jar that includes all the jars in the lib dir, so I don't have to copy the project jar and the lib dir into tomcat's ROOT dir. Can Netbeans do that ?

Answer1

That's really easy to package every dependent library (*.jar) into one single myProject.jar.

Just follow these steps and you will finally pack every dependent library into single jar. If you are using NetBeans then you can follow exactly or else you need to find your build.xml file in project files.

Follow these steps to edit build.xml

1) Click on Files tab on the left side of the project panel in NetBeans.

2) Double click on the build.xml file and add these lines in it just before  line

 
    
    
    
    
    
    
    
        
        
        
            
        
    
    
        
    
    

3) Change value in second line of the code as per your project name which is

 //<---Just value not name

4) Save it and right click on build.xml and choose Run Target and then Other Targets and finally click on Package-for-store

5) And here you done. Now you can go and check just like dist folder there will be a store folder which will be containing your final complete jar including all of your dependent libraries. Now whenever you want to change / add more libraries or so, just follow step 4.

Questioner:

love you man, worked like a charm, I was struggling in Neatbeans for a SINGLE jar. I was using FAT jar in Eclipse and was missing an alternative in Netbeans. Thanks again

 

Answer2

Robert Eckstein describes a solution for this problem. You just need to paste the following code to build.xml. The libraries are all found automatically.

Here we go:



    
    

    
    
    
    

    

    
    

    
        
        

        
            
        
    

    

    
        
    

    


  1. Go to your build.xml, and add the code right before the closing project tag at the end.

  2. Now change the value of the first propertiy field as commented

  3. Click Clean & Build, and your jar will be in the dist folder

Reference link: https://stackoverflow.com/a/30172829/2761273

Questioner:

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes

Answer3

  1. check MainClass main file is updated as main file in Properties>run
  2. Update Build.xml with following code before project tag closes

 















    
    

    
        
    





    



 

  1. Properties>packaging> tick all
  2. clean and build
  3. Upload to server.

Answer4

Okay, so this is my solution. I too had the problem with my program compiling and running on Netbeans but it failing when I tried java -jar MyJarFile.jar. Now, I don't fully understand Maven and I think this why was having trouble getting Netbeans 8.0.2 to include my jar file in a library to put them into a jar file. I was thinking about how I used to use jar files with no Maven in Eclipse.

It's Maven that compiles all the dependencies and plugins, not Netbeans. (If you can get Netbeans to do this please tell us how.)

[Solved - for Linux] by opening a terminal.

Then

cd /MyRootDirectoryForMyProject

Next

mvn org.apache.maven.plugins:maven-compiler-plugin:compile

Next

mvn install

This will create jar file in the target directory.

MyJarFile-1.0-jar-with-dependencies.jar

Now

cd target

(You may need to run: chmod +x MyJarFile-1.0-jar-with-dependencies.jar) And finally

java -jar MyJarFile-1.0-jar-with-dependencies.jar

Please see

https://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

 

 

 3. Building Java Projects

This chapter provides an overview of the building features in NetBeans.

This chapter contains the following sections:

  • About Building Java Projects

  • Working with Ant

  • Working with Builds

  • Building a Java Project

  • Using a Build Server

  • Compiling a Single Java File

  • Building a JAR File

  • Packaging an Application as a Native Installer

  • Preparing a JAR File for Deployment Outside the IDE

  • Using the Output Window

  • Refactoring Java Projects

  • Working with Maven in the IDE

  • Working with Maven Repositories

8.1 About Building Java Projects

NetBeans provides both Ant and Maven for building your Java applications. With Ant, if you are using a standard Java project, the IDE generates an Ant build script based on the options you enter in the project's Project Properties dialog box. If you are using a free-form Java project, the IDE uses your existing Ant build script.

With standard Java projects, you can customize the build process by doing any of the following:

  • Enter basic options, such as classpath settings and JAR filters, in the Project Properties dialog box.

  • Override IDE-generated Ant targets or create new targets in build.xml.

By default, the IDE compiles the classes in a standard project when you save them. This compile-on-save feature enables you to run or debug your applications in the IDE without having to wait for the projects to be built. However, the compile-on-save feature does not build your application JAR file. Before delivering your application to users, use the Clean and Build command to generate fresh versions of the project's distributable files.

For standard projects that have a main class specified, the IDE automatically copies any JAR files on the project's classpath to the dist/lib folder when you build the application. The IDE also adds each of the JAR files to the Class-Path element in the application JAR's manifest.mf file. This simplifies running the application outside the IDE. For more information, see Section 8.9, "Preparing a JAR File for Deployment Outside the IDE."

For information on how to customize an Ant build script, see Section 6.2.3.4, "Customizing the IDE-Generated Ant Script."

For information on how to modify a build JAR file, see Section 8.7, "Building a JAR File."

Maven is a framework that provides help with managing the project lifecycle, including building and managing dependencies. Maven projects follow a set of standards that are described with a Project Object Model (POM) file. You build the project using its POM and a set of plugins that are shared by all projects using Maven to ensure consistency between projects.

When you build, the IDE executes the plugin goals, builds the project and adds the project artifacts to the local repository. Maven uses repositories (local or remote) to contain a project's build artifacts and dependencies.

By adhering to convention, Maven frees you from having to explicitly specify every build action, configure the path to source files, and so on as it uses a default setup. Even though Maven is based on convention, you can customize a project by creating a custom configuration where you can map IDE actions to Maven goals enabling you to load the project in the IDE with a specific set of dependencies and trigger specific actions.

For information on using Maven in the IDE, see Section 8.12, "Working with Maven in the IDE."

For information on configuring Maven settings, see Section 8.12.2, "How to Configure Maven Settings."

For more information on Maven repositories, see Section 8.13, "Working with Maven Repositories."

8.2 Working with Ant

Apache Ant is a Java-based build tool used to standardize and automate build and run environments for development. Ant build scripts are XML files that contain targets, which in turn contain tasks. Ant tasks are executable bits of code that handle the processing instruction for your source code. For example, you use the javac task to compile code, the java task to execute a class, and so forth. You can use Ant's built-in tasks, use tasks written by third parties, or write your own Ant tasks.

For more information about Ant, see http://ant.apache.org/.

For information on installing Ant documentation, see Section 8.2.9, "How to Install Ant Documentation in the IDE."

8.2.1 Using Ant with the IDE

The IDE's project system is built directly on top of Ant version 1.9.0. All of the project commands, like Build Project or Run File in Debugger, call targets in the project's Ant script. Therefore, you can build and run your project outside the IDE exactly as it is built and run inside the IDE. For information on mapping Ant targets to debugger commands, see Section 6.2.4.6, "Mapping an Ant Target to an IDE Command."

You do not need to be familiar with Ant to work with the IDE. You can set all the basic compilation and runtime options in your project's Project Properties dialog box and the IDE automatically updates the project's Ant script. If you know how to work with Ant, you can customize a standard project's Ant script or write your own Ant script for your project. For more information on customizing an Ant script, see Section 8.2.8, "Ant Classpaths and Custom Tasks."

Note:

Though the IDE is built directly on top of Ant, the parsers that usually come with Ant are not necessarily bundled with the IDE. If you require parsers not included with the IDE distribution, you can add them to Ant's classpath using the Ant Settings > Additional Classpath property in the Options window.

For more help with the IDE's Ant support, see the NetBeans Ant FAQ at http://wiki.netbeans.org/.

For information on modifying the classpath for a project, see Section 6.2.3.1, "Managing the Classpath."

8.2.2 How to Edit an Ant Script

In standard projects the IDE generates the build script based on the options you enter in the New Project wizard and the project's Project Properties dialog box. You can set all the basic compilation and runtime options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant script. If you have additional requirements for the build process that cannot be handled in the Project Properties dialog box, you can modify the build script directly.

The main Ant script for a standard project is build.xml. The IDE calls targets in build.xml whenever you run IDE commands. This file contains an import statement that imports nbproject/build-impl.xml, which contains build targets that are generated by the IDE. In build.xml, you can override any of the targets from nbproject/build-impl.xml or write new targets. Do not edit nbproject/build-impl.xml directly, because that file is regenerated based on changes that you make in the Project Properties dialog box.

In addition, the build script uses the nbproject/project.properties which you can edit manually.

Editing an Ant script is much like editing any other XML file. All of the applicable Source Editor shortcuts are available. Double-click any of the Ant script's subnodes in the Files window to jump to that target's location in the Source Editor.

The IDE provides code completion for all standard Ant tasks. To enter an end tag for any empty beginning tag, type .

With standard projects, you can customize the build process by doing any of the following:

  • Entering basic options, like classpath settings and JAR filters, in the New Project wizard when you create a project, or afterwards in the Project Properties dialog box.

  • Editing properties in nbproject/project.properties. This file stores Ant properties with important information about your project, such as the location of your source and output folders. You can override the properties in this file. Be careful when editing this file. For example, the output folder is deleted every time you clean your project. You should therefore never set the output folder to the same location as your source folder without first configuring the clean target to not delete the output folder.

  • Customizing existing or creating new Ant targets by doing any of the following:

    • Add instructions to be processed before or after an Ant target is run. Each of the main targets in nbproject/build-impl.xml also has a -pre and -post target that you can override in build.xml. For example, to get RMI working with regular projects, type the code from Example 8-1 in build.xml.

      Example 8-1

      
        
                          
      
    • Change the instructions in an Ant target. Copy the target from nbproject/build-impl.xml to build.xml and make any changes to the target.

    • Create new targets in build.xml. You can also add the new target to the dependencies of any of the IDE's existing targets. Override the existing target in build.xml and then add the new target to the existing target's depends property. For example, code from Example 8-2 adds the new-target target to the run target's dependencies.

      Example 8-2

      
          
      
       
                        
      

      You do not need to copy the body of the run target into build.xml.

The following table lists some common tasks for redefining a JAR file that you may find useful.

To perform this task Follow these steps
Specify which files are added to a JAR file. Right-click the project node in the Projects window and choose Properties. Click the Packagingsubnode (under Build) and configure the filter and compression settings using the Exclude from JAR File field.
Change a JAR file's name and location. In the Files window, double-click the project's nbproject/project.properties file to open it in the Source Editor. Enter the full path to the JAR file in the dist.jar property.
Specify the manifest file for a JAR file. In project.properties, type the name of the manifest file in the manifest.file property. The file name must be specified relative to the project's build.xml file. Note that if you are using the Java Application template, the IDE creates a manifest file for you.
Disable the generation of a JAR file for a project. In the Files window, open your project folder and open build.xml. Override the jar target to have no contents and no dependencies. For example, add the following to build.xml:

 

The IDE automatically recognizes Ant scripts and displays them as Ant script nodes ( Ant script node icon) rather than as normal XML files. You can right-click Ant scripts in the Projects window, Files window, or Favoriteswindow to access a context menu of commands. You can also expand the Ant script node to see an alphabetical list of subnodes representing the Ant script's targets. Each of these subnodes also has a context menu of commands.

In the Projects, Files, and Favorites windows, an Ant script's subnodes are flagged in the following ways:

Icon Meaning
Emphasized Ant target icon Emphasized Ant target. These targets include a description attribute, which is displayed as a tooltip. You define the target's description attribute in the Source Editor.
Normal Ant targter icon Normal Ant target. A target without a description attribute.

 

Double-click any of the Ant script's subnodes to jump to that target's location in the Source Editor. All of the normal XML search tools, selection tools, and keyboard shortcuts are available for editing Ant scripts, and the IDE provides code completion for all standard Ant tasks.

When you create a target that you want to run from the command line, give the target a description attribute. Then, if you forget the names of the targets or what they do, you can run the ant -projecthelp

你可能感兴趣的:(JAVA)