Ant: target "install" does not exist in the project - java

I have to build two modules of the project using Ant. I have two xml files which I should use to build: project.xml and integration.xml. I use following command to build them:
ant install -f <filename>.xml
But the building (or installing) process ends successfully only for file project.xml. When I try command mentioned above with the integration.xml I receive:
Target "install" does not exist in the project "integration"
How should I handle this?

Look through your project.xml file and there will be a line like this there:
<target name="install"...>
Which is missing in the integration.xml, so in order to run task with the name "install" you need to add it to your buildfile.
You can look up further info here: https://ant.apache.org/manual/targets.html
Or you can just run
ant -p ${your_buildfile_name} to list all build targets in the build file.

The target that you give to ant (install in your case) refers to a definition in the build file.
In other words: one of your XML files contains an install target, the other does not.
So the solution is that you compare the two files to understand their differences. Either you have to add an install target to the second XML file, or you might simply have to use a different target with that one.

Related

Translate jardesc content into jar-command

I am making some WebFilters for our WebLogic server and I've got everything up and running.
The problem is how I package the filters into jar-files. If I use the following jardesc-file and create the jar-file from Eclipse, everything is working fine and WebLogic has no problems loading the webfilter class. But as soon as I try to manually create the jar-file using just jar.exe I am hitting ClassNotFoundExceptions when loading the webcontainer in WebLogic.
Working jardesc-file:
<?xml version="1.0" encoding="WINDOWS-1252" standalone="no"?>
<jardesc>
<jar path="C:/Workspace/Java/Jars/jars/corsfilter.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/CorsFilter/corsfilter.jardesc" exportErrors="false" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/>
<manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
<sealing sealJar="false">
<packagesToSeal/>
<packagesToUnSeal/>
</sealing>
</manifest>
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
<file path="/CorsFilter/.project"/>
<file path="/CorsFilter/.classpath"/>
<javaElement handleIdentifier="=CorsFilter/src"/>
</selectedElements>
</jardesc>
Current jar-command:
jar -cvf corsfilter.jar .project .classpath -C .\bin dk\akait\filters\cors\CorsFilter.class
The jar-command creates a jar-file that seems to be equivalent to the one generated using the jardesc-file in Eclipse, except for what looks like some kind of symbolic link to the META-INF-folder in the root of the jar-file.
Content of working jar-file:
Content of non-working jar-file
Can anyone explain what the right command for executing jar.exe is, given the jardesc-file?
Or
Can anyone explain what the META-INF file in the not working jar-file is?
Updated with output of jar-command run using jdk-1.8.0_111
As already mentioned in the comments. I would think this is more like a problem with WinRar as with the actually created jar-file. Probably eclipse doesn't use the jar command internally to create jar-files based on the jardesc descriptions. And the jar-files differ somehow in their internal structure.
However I'd suggest not to rely on the eclipse output, if you want to create a jar-file that you are going to distribute somewhere. I personally like gradle a lot and it is pretty easy to use.
Simply create a build.gradle file in you project root directory with the following content:
apply plugin: 'java'
// this is only needed, if you want to include the single file only
// by default all compiled files will be added to the output jar
jar {
include "dk/akait/filters/cors/CorsFilter.class"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.3'
}
Run
%PATH_TO_YOUR_GRADLE_BINARIES%/gradle wrapper
This will generate a local wrapper bat file that is used to ensure you are using the desired gradle version.
Run
%PROJECT_ROOT%/gradlew.bat build
and locate the jar-file under
%PROJECT_ROOT%/build/libs
You will however need to follow some conventions and place your java sources under a folder called src/main/java for this to work out-of-the-box. Or follow this documentation to setup different source folders.
I don't think your command line is even correct.
As according to jar output
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point]
[-C dir] files ...
And also http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html
It is expect to be [manifest-file] and [entry-point] after [jar-file] name.
Try
jar -cvf corsfilter.jar -C . .project .classpath -C .\bin dk\akait\filters\cors\CorsFilter.class

How to compile and run a Netbeans java project in command line without jar file?

I have a java Netbeans project having directory structure:
EMailQueue/
|-build/
|-classes/
|-mailqueue/
|-nbproject/
|-src/
|-mailqueue/
|-dist/
|-lib/
|-build.xml
|-maifest.mf
build/classes/mailqueue/ directory consists of all the classes compiled from all the .java files in src/mailqueue/ directory and dist/lib/ consists of all the .jar files (except JDK) imported in the application's files.
I want to know, how to run this application without using the jar file present inside the dist/ directory.
Also I want to know if I do not have classes files with me then how to compile .java files in src/mailqueue/ to generate class files in build/classes/mailqueue/ directory.
PS: Please give the exact command to run in terminal and optionally any explanation.
To compile Netbeans project You need to call Apache ant tool and pass them your build script file thorough -f parameter like this
ant -f ../apppath/build.xml
this will do full project with building jar file.
If You look at build.xml file You will see that it's only points to ./nbproject/build-impl.xml file where all required actions are actually defined.
Once no parameters passed to ant it calls default target
<target depends="test,jar,javadoc" description="Build and test whole project." name="default"/>
You see that this task depends of three other. Most interest is 'jar' target.
Look at this target - it depends of other 'compile' target
To call this target directly You need to pass this target to ant tool
ant -f ../apppath/build.xml compile
This will compile project but not to pack them to jar
ant -f ../apppath/build.xml run
will compile project and run main class
Hope this helps.
To compile Your code with pure java You need at first to run java compler in folder with all java files i.e call javac.
javac -cp {path to all java library files and external class files} *.java
Once You have all compiled with no errors run java same way and pass them Your main class name You want to run
java -cp {path to all java library files and external class files} ClassName

How to add directory to Clojure's classpath?

I have installed the libraries with Maven to the ~/.m2/repository/ directory. I would like to add that path to the default Clojure classpath. I could not find the documentation how to do that.
Any hints?
Cheers!
clj
Clojure 1.4.0
user=> (require '[clojure.java.jmx :as jmx])
FileNotFoundException Could not locate clojure/java/jmx__init.class or clojure/java/jmx.clj on classpath: clojure.lang.RT.load (RT.java:432)
The class path by default is:
user=> (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
(#<URL file:/Users/myuser/cljmx/> #<URL file:/usr/local/Cellar/clojure/1.4.0/clojure-1.4.0.jar> #<URL file:/Users/myuser/cljmx/>)
nil
Leiningen really makes this process a lot less painful by keeping the setting of the classpath associated with the project, and more importantly leads to a repeatable build process. where you can come back to the project years later and still get a repl. A general outline of using leiningen in these cases:
lein new projectname
add the library you need to your project.clj file with a name you choose
run lein deps to print out the command to use to add the jar to your local repo
add the jar
run lein deps again (you can skip this step if using leiningen2)
run lein repl
enjoy
this is assuming that the library you are using is not already part of or available from a package in a maven repo, which many are.
The non-painful, popular method is to not mess with maven and classpaths and the JRE directly and use leiningen: https://github.com/technomancy/leiningen/
Otherwise, you can modify whatever is in clj and add/set the classpath in whatever ways java likes. See for example Setting multiple jars in java classpath
It should be noted that you also have the option of adding classpaths at runtime with the library pomegranate https://github.com/cemerick/pomegranate
This lets you do like:
(require '[cemerick.pomegranate :as pom])
(pom/add-classpath "/home/user/~.m2/....")
I assume that clj is a script to start Clojure REPL. Take a look into this script and find line similar to this:
java -cp /path/to/clojure.jar clojure.main
Here you start class clojure.main having "clojure.jar" on your classpath. To add more jars just add them to the end of -cp option values. E.g. on Linux:
java -cp /path/to/clojure.jar:/path/to/mylib.jar clojure.main
(use ; instead of : on Windows)
However, very soon you'll get tired of this way and will look for project management tool. So it makes sense to start using it right now. Take a look at Leiningen - it manages dependencies for you based on Maven (so it will be extremely easy to add new jar) and has REPL.
Another option is to create a deps.edn file in the folder where you plan to run the REPL or where you keep your project files.
This file is used to inform Clojure about dependencies, source files, execution profiles, etc… It's loaded when you run a REPL (but there are other use cases) and it's supported by the core of Clojure and it's officially documented on https://clojure.org/reference/deps_and_cli
In your case you may just want to put something like the following, to declare what dependencies you want to download and put on the Java classpath.
{
:deps {
the.dependency/you-want {:mvn/version "1.0.0"}
}
}
In deps.edn you can specify:
third party dependencies (eg JARs) that can be already saved locally, or hosted on a Maven repository, or on Git repository…
source paths, where your source code resides, if any
Note that the dependencies will be downloaded and cached in .cpcache/ folder, beside the deps.edn itself. I'm not sure if you can instruct it to use the global ~/.m2 instead.
You may find the dependency coordinates (name and latest version) on clojars.org
deps.edn is "lighter", part of the core Clojure, if less powerful than leiningen; so maybe suited for setting up an environment for casual/exploratory coding at the REPL or CLI.
You can also have a global deps.edn in ~/.clojure/deps.edn where you may want to define common configurations, dependencies, etc. to be used across different projects. Specific configurations can be invoked/overridden using options on the command line.

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, especially as the order of dependencies may be important and needs to be preserved from the order in the ivy config.
Has anyone done this before? I also need to generate relative paths in the classpath so I can't use absolute paths as this will only work for the machine on which the build is done.
EDIT: Based on feedback if we cut Ivy out the equation (do the resolve to a directory of my choice) I can then probably resolve the list of libs ok. But how would I generate a classpath suitable for a start script, especially with the relative paths (relative to my bin directory)?
e.g.
install
/bin <-- scripts here
/lib <-- jars here
So in my bin/start.sh I need to have ../lib/ in front of every jar reference rather than a full absolute path.
Thanks.
Since many years (2000?), we had this small script in path ("make_cp")
#!/usr/bin/perl
my $CLASSPATH="";
my $DIR=shift;
$DIR||="lib";
opendir(LIBDIR, $DIR);
while ($file = readdir(LIBDIR)) {
$CLASSPATH.=":$DIR/$file" if ($file =~ /\.jar$|\.zip$/);
}
closedir(LIBDIR);
$CLASSPATH=~ s/^://g;
print "$CLASSPATH";
Used like this:
export CLASSPATH=`make_cp lib`:`make_cp external-lib`
Since Ivy evicts overlapping dependencies and tries to find the best common dependency for all the projects I don't really understand how the order of dependencies would matter at all.
However you should make a standard JAR/WAR/other with Ant for your project and include Ivy dependencies inside that JAR. Basically all you should need to do is to make Ivy's Ant task to resolve the dependencies to a folder, then build tha classes using those dependencies and then consruct the JAR so that you include the library JAR:s to newly created JAR's /lib/ folder.
Like Esko said, you should create a JAR including all required JAR archives:
<zip destfile="abc.jar">
<zipgroupfileset dir="lib/distributed" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="com.acme.MyClass"/>
</manifest>
</zip>
After that, your start script is simply:
java -jar abc.jar
If you're using java 1.6 you can use file globs (i.e. java -cp "../lib/*"). If you're using an earlier version of java and you don't want to use Vladimir's solution, you'll need to write a script that figures out what the classpath should be.
So launch.sh looks something like:
cd dirname %0 # change to the bin directory, use %0/.. instead and you can replace ../lib with just /lib
sh set_classpath.sh # set the classpath
java -cp $CLASSPATH some.package.Main
and set_classpath.sh will have some linux magic that sets CLASSPATH equal to something like "../lib/abc.jar:../lib/def.jar"
export CLASSPATH=`ls *.jar | sed 's/[^.jar].jar/..\/lib\/\0:/'`

Build project into a JAR automatically in Eclipse

I have an Eclipse project where I want to keep my Java project built into a JAR automatically. I know I have an option to export the project into a JAR; if I do a right click; but what I am really looking for is, that like Eclipse automatically builds a project's .class files and put them in target folder; it should also build a JAR automatically and copy the latest JAR at some or a specific location.
Is there a option to configure Eclipse in such a way, to build JARs automatically?
Just to make it clear for guys, patient enough to answer my question; I am not looking at ANT as solution; as I already use it, but what I would like it something that gets initiated automatically either with a time based trigger or immediate build with change.
You want a .jardesc file. They do not kick off automatically, but it's within 2 clicks.
Right click on your project
Choose Export > Java > JAR file
Choose included files and name output JAR, then click Next
Check "Save the description of this JAR in the workspace" and choose a name for the new .jardesc file
Now, all you have to do is right click on your .jardesc file and choose Create JAR and it will export it in the same spot.
Create an Ant file and tell Eclipse to build it. There are only two steps and each is easy with the step-by-step instructions below.
Step 1
Create a build.xml file and add to package explorer:
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="." includes="*.class" />
</target>
</project>
Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.
Step 2
Right-click on the root node in the project.
- Select Properties
- Select Builders
- Select New
- Select Ant Build
- In the Main tab, complete the path to the build.xml file in the bin folder.
Check the Output
The Eclipse output window (named Console) should show the following after a build:
Buildfile: /home/<user>/src/Test/build.xml
CreateJar:
[jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds
EDIT: Some helpful comments by #yeoman and #betlista
#yeoman I think the correct include would be /.class, not *.class, as most
people use packages and thus recursive search for class files makes
more sense than flat inclusion
#betlista I would recomment to not to have build.xml in src folder
Check out Apache Ant
It's possible to use Ant for automatic builds with eclipse, here's how
This is possible by defining a custom Builder in eclipse (see the link in Peter's answer). However, unless your project is very small, it may slow down your workspace unacceptably. Autobuild for class files happens incrementally, i.e. only those classes affected by a change are recompiled, but the JAR file will have to be rebuilt and copied completely, every time you save a change.
Regarding to Peter's answer and Micheal's addition to it you may find How Do I Automatically Generate A .jar File In An Eclipse Java Project useful. Because even you have "*.jardesc" file on your project you have to run it manually. It may cools down your "eclipse click hassle" a bit.
Using Thomas Bratt's answer above, just make sure your build.xml is configured properly :
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="bin/" includes="**/*.class" />
</target>
</project>
(Notice the double asterisk - it will tell build to look for .class files in all sub-directories.)
Creating a builder launcher is an issue since 2 projects cannot have the same external tool build name. Each name has to be unique. I am currently facing this issue to automate my build and copy the JAR to an external location.
I am using IBM's Zip Builder, but that is just a help but not doing the real.
People can try using IBM ZIP Creation plugin.
http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download

Categories