include text file in dist folder - java

Is there any way that in the dist folder (when doing clean and built) I include a text file necessary for the program to work in netbeans? It's because I'm forced to copy it by hand every time I generate the dist folder when building. I want the text file to be added to the root of the project. I am using "apache netbeans ide 14" and using ant. And this is the code of the build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="pesca1.6" default="default" basedir=".">
<description>Builds, tests, and runs the project pesca1.6.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="pesca1.6-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
I need the text file, necessary for the program to work, to be automatically added to the root of the dist folder when I build the project.

Text file in dist folder looks very strange, IMHO.
I would recommend you to check nbproject folder, especially build-impl.xml file. This file describes how exactly build process is going.

Related

Custom manifest added multiple times in -post-jar in a common dependency

I have a multi-module native Netbeans Java EE project. In it I have a Java Class Library project that is used by multiple other projects which in turn are packaged into the root .ear project.
I'm adding the "build timestamp" and the "build user" attributes to a custom manifest using the library's build.xml:
<target name="-post-jar">
<jar destfile="${dist.jar}" update="true">
<manifest>
When I "clean and build" the root project, each project that refers the library calls:
<ant antfile="${call.script}" target="jar">
And my -post-jar target is called multiple times. This wouldn't be a problem, but sometimes the second invocation of the <jar> task fails with Unable to rename old file (probably due to Netbeans scanning the files in background, but I can't tell for sure).
There are repeating pairs of Building jar and Updating jar messages in Ant's output. However, if I remove my -post-jar target, the second invocation of the jar target does nothing, because it thinks that the jar is up to date and I see only one Building jar message.
How do I mark the updated jar up to date, so the second invocation of the jar target does nothing?
There's a github repo that demonstrates the problem.
I haven't found a way to not re-generate the manifest every time, but I found a way to make the generated file look the same as the zipped file (and we know that the <jar> task doesn't repack when the contents are the same).
Instead of updating the zipped manifest in -post-jar I now update the source file in -pre-jar. This way the final version of the manifest is zipped and since its contents don't change during build, subsequent <jar> invocations update nothing.
It worth mentioning that before adding the attributes Main-Class, Profile, etc. the build-impl.xml of Netbeans creates an empy manifest template, if the user doesn't provide a valid path in the manifest.file= property. The addition happens after -pre-jar, however the existence of the user-provided manifest is checked much earlier, during the init target and the result is saved to the manifest.available property.
My manifest template is not a static file. It contains the "build timestamp" and the "build user" attributes. Therefore the file doesn't exist during the init target, so I had to add the following line at the beginning of my build.xml:
<property name="manifest.available" value="true"/><!-- It will be available by the time we need it -->
Secondly, manifest.file still has to be set and I set it in project.properties (there's no UI for that setting yet and I wonder how it would behave in the presence of the variable in path)
manifest.file=${build.dir}/manifest.tmp
Next, I overwrite the manifest template in the -pre-jar target:
<tstamp>
<format property="current.time" pattern="HH:mm:ss" locale="de,DE"/>
</tstamp>
<target name="-pre-jar" >
<manifest file="${manifest.file}">
<attribute name="MyApp-built-time" value="${current.time}"/>
<attribute name="MyApp-built-by" value="${user.name}"/>
</manifest>
After that, the new problem became obvious: the timestamp was different for each invocation of
<ant antfile="mylib/build.xml" target="jar">
in the multi-module project and Ant had to repack the jar with the new timestamp in the manifest. I solved this by defining the timestamp property in every project's build.xml. Although the properties are not inherited due to inheritall="false", Netbeans allows for overcoming that:
<property name="transfer.current.time" value="${current.time}"/>
This mechanism is broken in Java EE projects, but the workaround is simple:
<presetdef name="ant">
<!-- workaround transfer.* not working in Java EE projects -->
<ant>
<propertyset>
<propertyref prefix="transfer."/>
<mapper from="transfer.*" to="*" type="glob"/>
</propertyset>
</ant>
</presetdef>

Eclipse IvyDE Dependency Variable

I would like to define an ivy dependency using an ant variable. Right now I have this:
<dependency org="my.org.name" name="jar_name" rev="${release_version}"/>
In my ivysettings file I have:
<property name="release_version" value="${ANT_RELEASE_VERSION}"/>
One of the first tasks executed by my ant script is this:
<target name="getVersion">
<echo message="Getting current version number" />
<java classname="my.org.Version" >
<classpath>
<pathelement location="${SOME_PATH}/lib/some-jar.jar"/>
</classpath>
<redirector outputproperty="ANT_RELEASE_VERSION" />
</java>
<echo message="Version: ${ANT_RELEASE_VERSION}" />
</target>
Now this WORKS in runtime. When I execute my tasks, everything goes as expected and I get the correct version number. The issue is, eclipse is unable to compile the classpath due to the following error:
${ANT_RELEASE_VERSION}: not found
I tried solving this issue by defining an eclipse String substitution variable under:
Window -> Preferences -> Run/Debug -> String Substitution
I set:
ANT_RELEASE_VERSION = 4.4.9
Yet eclipse is still not able to resolve my dependency, which results in my project never being able to compile (in eclipse). Is what I'm trying to do possible? Is there a way to get eclipse to compile without hardcoding the version number in my ant script/ivy settings file?
Try to create an Eclipse property via :
Window > Preferences > Ant > Runtime > [Tab] Properties
and create a property named ANT_RELEASE_VERSION
to make it work with
ant scripts started within Eclipse.
You can create a properties file inside your Eclipse project and refer it from projects Ivy settings.
Create a new .properties file in your project.
Put your properties in this file. In your case, you could create the property 'release_version' and assign to it the desired version number.
After that open the Ivy Classpath Container properties dialog by selecting 'Properties' from its right-button menu.
Next, open 'Settings' tab. Check 'Enable project specific settings' checkbox. In 'Property files' list add the properties file.
Press 'Ok'.

Java: How can I compile an entire directory structure of code in command prompt? [duplicate]

This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 8 years ago.
I'm using Eclipse & Tomcat7 in window7 platform, I have configured project in eclipse also. Usually we run the single java file using CMD.
But I want to compile and run the entire java code through command prompt.
I've a many structures in single src folder like E:\proj\src\com\mycode.Inside mycode folder there are 7 sub-folder are available & each sub-folder have many .java files & inner-sub-folders.
For Example:
E:\proj\src\com\mycode\dto\mail.java,E:\proj\src\com\mycode\dto\sms.java
E:\proj\src\com\mycode\dto\security\securityFile.java
The above same pattern other folders have java files.so I need to compile & run entire java files including sub-folders & inner-sub-folders USING COMMAND PROMPT.
thanks in advance,
I'll make some (hopefully reasonably safe) assumptions about how your code is structured:
you have a main program (I call it com.mycode.dto.Main below),
it has compile-time dependencies on the other files (you're not using reflection or whatever),
your source files match the package structure (com.foo.Bar is in E:\proj\src\com\foo\Bar.java).
In that case you can do:
javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java
and then the compiler will traverse the file dependencies automatically and output the class files to destination.
If you have sources in multiple hierarchical directory, you may use ant.
create a build.xml file in the root of your project directory.
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Install ant on your machine, be sure to have its bin directory in your path, then you can just run
ant -f build.xml
Of course this is just a starting point (ant offers several interesting options and let you finetune all the aspects of your build/packaging).
the sample build.xml file was taken from here
Try using wildcard:
javac *.java
You could use various parameters as per need basis that comes in handy with javac:
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system

Stopping only a portion of build script

I have run into a problem regarding the build script. Presently I have a main build.xml file that calls each internal build.xml files from a directory. The internal build.xml has 2 stages to execute wherein at the end i get a jar file.
My requirement is that if anything foes wrong in the 1st step of the internal build.xml file, i should not get the jar file, but the main build should continue execution and go to the next internal build.xml file.
In the internal build.xml based on some condition i want to stop that internal build.What changes should i make in my code to stop that particular build?
Thanks in advance.
Set failonerror="false" on your subant tasks you're using to call the other build files.
If you can afford to add ant-contrib to your build, then it is easy. In your main task, do:
<target name="xxx">
<var name="firstStepOK" value="true"/>
<trycatch>
<try>
<!-- call first sub build -->
</try>
<catch>
<var name="firstStepOK" value="false"/>
</catch>
</trycatch>
<!-- call second sub build, pass value of firstStepOK -->
</target>
Unfortunately, ant-contrib has never made it into ant proper, which is a pity. It contains very useful tasks.

Netbeans - adding resource files to jar file with Ant

I want add some resource files (non-code text-based files) to the jar file in a Java project using Netbeans 6.9, I'd expect using Ant. I had thought that this would be reasonably simple...but after quite a bit of searching I can't find how to do it..! Any pointers in the right direction?
The answer I think I was looking for is as follows:
In the build.xml file (as per trashgod's answer) you can use the hooks already in the ant build script to add the following:
<target name="-post-jar">
<echo>Adding files to jar...</echo>
<jar destfile="dist/jarFileName.jar" update="true">
<fileset dir="${basedir}">
<include name="files/*"/>
</fileset>
</jar>
</target>
This adds the files directory and any files under it directly to the jar file.
If you choose File > Project Properties > Build > Packaging, you'll see a dialog that lets you exclude artifacts from the build; everything else is the source tree is included. The source of TreeIconDemo is a concrete example that inlcudes html files.
For more advanced tasks, examine the default build.xml generated for a freshly created project; it identifies various hooks into the predefined tasks. For example,
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
Addendum: As an example, this target overrides -post-compile to print some statistics.
<project name="TreeIconDemo" default="default" basedir=".">
<import file="nbproject/build-impl.xml"/>
<target name="-post-compile">
<echo>build.dir: ${build.dir}</echo>
<length mode="all" property="build.size">
<fileset dir="${build.dir}">
<include name="**/*"/>
</fileset>
</length>
<echo>build.size: ${build.size}</echo>
</target>
</project>
Output:
$ ant compile
Buildfile: build.xml
...
-post-compile:
[echo] build.dir: build
[echo] build.size: 11992
compile:
BUILD SUCCESSFUL

Categories

Resources