I'm using ant to build a project in eclipse. Before the jar is constructed, I want to move some files from another project into a resources folder in this project.
The problem I'm having is that none of the files show up in the jar.
Example:
<project>
<target name="A">
<copy file="../otherloc/file1" tofile="resources/file1" />
<waitfor><available file="resources/file1" /></waitfor>
</target>
<target name="B" depends="A">
<jar destfile="dist/jarfile.jar">
... (actually build the jar)
</jar>
</target>
</project>
So in the above case, "file1" doesn't actually make it into the jar, even though everything else in the resources directory does. Eclipse doesn't show the new file in the resources directory until I refresh, either. It's like, because eclipse doesn't know about it, it's not included in the build.
Any suggestions appreciated. I'm pretty much an Ant noob, so it's possible I'm going about this entirely wrong.
Edit: Alternately, is there some way to just include the file from the other project? I was a little unclear on the best way to get it into the jar. It needs to be in a directory along with some other existing project files.
Did you check both tasks independently?
First to have file1 copied in the proper location.
Then, if B fails to put file1 in the archive, the issue could lie in the tag. Are you sure resource/file1 is handled by a fileset ?
Unfortunately we haven't been able to resolve this, so we just work around it by separating the task into two steps and copying the files before running the build script. Not really a great "answer", but nothing else has been suggested.
Related
I'm using netbeans 7.2 with NBAndroid extension. In my android project, I'm referencing a library (ActionBarSherlock) that is not in jar (can't be, for some reason). The problem is, that netbeans doesn't see classes from that library and gives me errors (package does not exist etc.) However it builds and runs OK, the library is added correctly. Netbeans just doesn't see it.
Here is a screenshot.
Here is similar question, no solution package com.actionbarsherlock.app does not exist
Is there a way to fix this? Thanks for help!
EDIT: So I found a way to solve this, it's more a workaround than a solution. I created a jar file from the library classes called classes.jar. I put it in the libs folder, so netbeans sees it. Than I created custom_rules.xml (it's imported via build.xml). In it I move classes.jar away from the libs folder, so I can build it, and in the end I move it back.
<?xml version="1.0" encoding="UTF-8"?>
<project name="imported">
<copy file="libs/classes.jar" todir="./" />
<delete file="libs/classes.jar" />
<target name="-post-compile">
<copy file="./classes.jar" todir="libs/" />
<delete file="./classes.jar" />
</target>
</project>
The errors you see are because NetBeans doesn't recognize the packages, classes, methods, etc. When you build or run the program, NetBeans resorts to the Android project's Ant script, which apparently is configured correctly to find the classes. As far as compiling and running, you won't have a problem. However, if you want to use NetBeans' autocomplete and error-detection features, you need to configure it to detect your libs. To do this, just right click on your project name in the Project pane and click Properties from the context menu. Next, click on Libraries under Categories on the left. Then click Add JAR/Folder and navigate to the folder with your third-party library. You can select one of the options for the path then click OK. Now NetBeans should be able to find the identifiers and help you write your code.
Rather than doing some tricks in your build script I'd recommend you to use ActionBarSherlock as a library project. You should be able to do this in project customizer (select project, right click, choose properties).
Also see 'Including in YOur Project' bullet 2. in http://actionbarsherlock.com/usage.html
-Radim
I hope somebody can give me an advice...
Problem
In source directory of my project some packages contain resources which are not ".java"-files. Now I need to put there compiled ".class"-files. The problem is that ANT filters them out when I am building the final JAR. ".dll" and ".png" files are not filtered out.
How can I achieve that everything is copied to the target directory?
I use following Ant task:
<javac srcdir="${src}" destdir="${temp}" target="1.5" encoding="8859_1" debug="${compile.withdebug}" >
<classpath refid="libs_path" />
</javac>
Background
I had to put in the final JAR multiple other OS-dependent JARs (SWT distributions). Only one JAR would be loaded at a program start. However it is not possible to let JVM load JAR from JAR without any special class loader. So I extracted all JARs to a package and JVM can load them now.
Why I want to put them under source (in Java package)? Because I want reference them relatively to the helper Java class:
org.example.swt_jars\
swt_linux_32\
swt_linux_64\
swt_win_32\
swt_win_64\
SWTJarsResources.java
Thanks!
Javac is a compiler; getting it to move files around is using the wrong tool for the job.
I would suggest simply using an Ant copy task directly before or after the <javac> task to move existing class files across. Something like:
<copy todir="${temp}">
<fileset dir="${src}">
<include name="**/*.class" />
</fileset>
</copy>
EDIT for Eclipse: what you're really trying to do here isn't have Eclipse copy the files, but for it to recognise that there are classes there that it needs to reference. So the simplest approach and one that I'd try first is to mark your src directory as a location that contains classes as well as one that contains sources.
I don't know if this would work for Eclipse - IDEA for example doesn't let a folder act as dual-purpose in this way. And again, it doesn't seem like it's quite the right tool for the job, as an IDE build is just collating the binaries it needs to run the app, and copying files around based on some project settings seems wrong somehow.
Ultimately I don't think your design is the cleanest, mixing classes in with source files is likely to be confusing. I appreciate that you're doing this because you want to use relative references, but perhaps you ought to abstract this out from the filesystem itself and use Classloader.findResource() (or probably getResourceAsStream()). This way you could arrange the files however you want, and so long as you run your application with both directories on the classpath, Java will be able to find the resource you're after. This will give you more flexibility in general, as well as solving this particular situation.
I'm working on a multiproject solution for a client and we're trying to have all our builds automatically go into a predetermined BIN folder. All the C++ projects were easily enough moved over but the Java side has proven not so easily configured. When I go to the project's properties and go to Build->Packaging the "JAR File:" text box shows a read-only text-box pointing to "dist/App.jar" (which I would like to change to "../../../bin/App.jar". Any thoughts on how to do this?
You can change the dist-folder by editing the dist.dir key in the project.propertiesfile which is located in the nbproject directory from
dist.dir=dist
to
dist.dir=../../../bin
In my case the key is in line 24. The name of the jar-file itself is determined by the dist.jar key, if you would need to rename the file aswell.
add into build.xml
<target name="-post-jar">
<copy file="${dist.jar}" todir="drive\path\"/>
</target>
Did you check under the menu
[Run] --> [Set project configuration] --> [Customize]
You have plenty of options to change there, it is easy than that.
Good luck
I have one Example.war file, which dont have the src code of that war file.but i wanted to change some thing to the example.war file. so i created new page (.java file) and compiled the page through ant..it was build successfully.now i want to append the new page(.class file) to the Example.war.I just opened the example.war file with the help of winZip and added those .classfiles to the war file.but the thing is it doesnt showing any changes after deploying the war file through tomcat.please let me know whether i followed the correct process r not.If not kindly tell me the exact way of appending the .classfiles to the war file.
You cannot use WinZip. Well, you can, but there's a manifest file you need to update, every time you change the set of .class files in the .war.
Rather than do it manually, the simple way to do it is to use the jar command that comes with the JDK.
Unpack the war (with any tool, but jar will let you do it), place your .class file into the lib directory, then repack the war file with the jar tool.
This page shows how to create a war from within ant.
basically,
<target name="war" depends="compile">
<war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent"/>
<lib dir="WebContent/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
The first answer to your question is "Yes, you can add class files to a war file". When you say new "page", do you mean new servlet (I assume you do, since you said it was a Java class and not a JSP file)?
If you wanted to compile a new class file into an existing war, you would take the following steps:
Make sure the java version you are compiling it with using ANT is the same as the java version your application server is using (the one hosting the WAR file).
Compile the .class file in ANT, like you are doing.
You can use any zip editor (WinZip -- although I prefer WinRAR) to open up the war file and put the .class file in the WEB-INF/classes folder.
However, at this point you haven't told the web application about your new class file (if it is a servlet). The final step you will need to take is to edit the web.xml file in WEB-INF to map your servlet to a path. See this article for an example.
You can see some more details about this whole process here.
Finally, you may have to clean out the cache for your application server and restart it. if you are using Tomcat, the easiest way is to delete everything in the "work" directory and restart the application server.
I'm working with Android at the moment, trying to avoid using Eclipse (for which I have an irrational hatred).
I need to include an external .jar file (used in my Activity)and have no idea how to link it for ant debug...
I've read up on build.xml files but adding <path id="compiler.classpath">...</...> or <classpath> nodes to the XML doesn't help fix it.
Hope someone can help me out!
Just put it in the libs/ directory. Everything else is taken care of from there -- no Ant script modifications are needed. For example, here is a sample project showing integrating a BeanShell interpreter this way.