Ant: Unzip patternset not excluding META-INF and jUnit - java

Because some guys forgot to set the junit scope to "test", it has been packaged along with our libraries. Since we don't need junit in our final product, I wanted to exclude the class files in an unzip task.
Note: This task is necessary in the further build process, so leaving this out is not an option.
So far my unzip statement looks like this:
<unzip dest="${classes.dir}">
<fileset refid="dependency.fileset"/>
<patternset>
<exclude name="META-INF/*"/>
<exclude name="org/junit/**/*.class"/>
<exclude name="junit/**/*.class"/>
</patternset>
</unzip>
I tried various combinations, but the junit and META-INF files magically reappear each time.
As a work-around I added a delete statement with a fileset. It works but is completely unnecessary if I have the option to add a patternset to the unzip statement - in my opinion:
<delete includeemptydirs="true">
<fileset dir="${classes.dir}" casesensitive="false" includes="META-INF/*,junit/**/*,org/junit/**/*" />
</delete>
I already read through the manual, but found no clue on how to solve this problem.
Did I miss something, are the patterns incorrect or is it something else?

Related

Java Ant Job how to ship Log4j jar with my jar

Just trying to upgrade some old stuff and part of that I need to bundle my custom jar which uses Log4j. I did add the following for my <javac> task which compiles successfully.
<path id="my.classpath">
<fileset dir="${mainpath}">
<include name="**/*log4j*.jar"/>
</fileset>
</path>
<javac srcdir="src/java" destdir="build/filez/java" debug="on" deprecation="no"
includes="my/instruments/**/*, org/apache/log4j/**/*">
<classpath refid="my.classpath"/>
</javac>
However, In my <jar> job I cannot see any log4j dependency packed with my custom jar. This might be a silly question, but how do ensure that my custom-jar does not fail when called from another application since the dependency isn't packed? Will it be okay as long as log4j has been loaded by classloader in the target application?
Additionally, do I need to add something in my Manifest for this?
I cannot use Maven (yes I know) for a little while, so cannot solve this problem with maven
You can use One-JAR to package your code along with it's dependencies into one big executable JAR.
It can be used either as a standalone tool from the command line or as a task defined in build.xml.
<!-- Construct the One-JAR file -->
<one-jar destfile="hello.jar" manifest="hello.mf">
<main>
<!-- Construct main.jar from classes and source code -->
<fileset dir="${classes.dir}/src"/>
</main>
<lib>
<fileset file="${build.dir}/lib.jar" />
</lib>
</one-jar>

Ant can't find classpath classes

So I'm extending my company's ant build script to add in a special module we want build in some cases. I've written an ant script that points to where I know the compiled class files for the rest of our codebase are, because they get compiled earlier in the build process. I know with 100% certainty the files are in this location.
However, whenever I try to compile this module, the classpath reference can't see those classes, and I get a bunch of "package does not exist" and "can't find symbol" errors.
I just can't seem to figure out what I'm doing wrong. Hoping for help here.
Here's my build script code:
<property name="classpath" value="${dir.dev}/out/production/Main"
<path id="pfClasspath">
<fileset dir="${classpath}">
<include name="**/*.class"/>
</fileset>
<fileset dir="${dir.dev.lib}">
<include name="**/*.jar" />
</fileset>
<fileset file="${lib.json}" /> <!-- TODO try removing this -->
</path>
<target name="compile" depends="prepare">
<javac source="1.7" classpathref="pfClasspath" srcdir="${dir.project}/src" destdir="${dir.project.build.classes}" />
</target>
The directory the "classpath" property is pointing at 100% contains all of the class files for the rest of the project. That level is the equivalent of the "src" directory on the sources side, immediately within it are the com/companyName/etc... folders.
My code contains references to the classes compiled at this location. Yet ant isn't finding them. Any help?
Try
<path id="pfClasspath">
<pathelement path="${classpath}" />
...
</path>
instead. Specifying the classpath does not mean to specify every single class file that's on the classpath, which is what you do when you define your <path> element using a <fileset>.

log4j.properties configuration issues along with ant configuration

I've spent a lot of time reading the posts on stackoverflow regarding log4j and the different ways it can be implemented. I've decided to take the approach of log4j.properties. I am still running into issues when I run from within eclipse or from ant with:
log4j:WARN No appenders could be found for logger (My.Class).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I've done two things thus far:
Created the log4j.properties file in the root of the /src folder
Moved the log4j.properties file into the package where the .java file exists.
Both instances produce the same issue. Here is the code I am using in my class:
private static Logger log = Logger.getLogger(My.class);
I have read over and over that the properties file needs to be in the classpath. I feel like i have done that, but maybe not. Here is my package structure:
src
packageA
myclass.java
log4j.properties (attempt 2)
packageB
packageC
packageD
log4j.properties (attempt 1)
Let me point out I'd like every java file in all the packages to use the same log4j.properties. If there is an easier way to configure this other than redundantly coping props files around let me know. I was thinking I'd have to switch to using a resource loader.
Moving on to my ant issue:
I have the exact same issue in ant. I have added what I thought I needed from what I read on apache's site to get ant to run w/o issue, but to no avail.
here is the additional entry in my compile target...
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar">
<include name="**/*.properties"/>
</fileset>
</path>
<target>
<javac srcdir="${source.dir}" destdir="${classes.dir}" classpathref="classpath" debug="true" debuglevel="lines,vars,source" includeAntRuntime="false"/>
<copy todir="${classes.dir}">
<fileset dir="${source.dir}" excludes="**/*.java"/>
</copy>
</target>
I should also note my junit target has the included classpath.
I also attempted adding the -Dlog4j.configuration=file:/path to file and I still see the warnings after that... I'm at a loss.
I was under the impression this would copy my .properties file into the classpath of ant.
for ant i added:
<path id="properties">
<dirset dir="${config.dir}"/>
</path>
where ${config.dir} is the path to the /src/config folder where the log4j.properties exists.
Still working on getting eclipse to work, it shouldnt be too different with classpaths in eclipse.
Thanks!
What I've found is that my log4j.properties must be in the root of the classpath.
Since my classpath src includes test code in the applybc2014 package, I've put my log4j.properties there. Note, I'm also excluding ONLY java files.
<property name="src.dir" value="src/ca/bccampus/tests/applybc2014"/>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" classpathref="classpath"/>
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>

Why my Ant delete does not work?

Using Ant 1.8.0
<target name="main">
<delete includeEmptyDirs="true">
<fileset dir="target/xxx/WEB-INF/lib" casesensitive="yes">
<filename name="junit-*.jar"/>
<filename name="gin-*.jar"/>
</fileset>
</delete>
</target>
When I run this Ant script, nothing happened, if I leave only one seem it works. I checked out the Ant FileSet Type, http://ant.apache.org/manual/Types/fileset.html, seem two does not matter.
So anybody who can tell me what's the problem here?
The delete isn't succeeding because your files don't match both of the filename selectors you specified. From the docs:
If any of the selectors within the FileSet do not select the file, the
file is not considered part of the FileSet. This makes a FileSet
equivalent to an <and> selector container.

Ant copy classpath jars to a directory

I'm sure this has either been asked before or is pretty straightforward. But for whatever reason, I cannot seem to make it work. I want to use ant to copy the ${build.classpath} (which contains a colon separated list of jars) to the ${output.dir}/myapp/WEB-INF/lib.
I have this right now and it doesn't seem to work:
<copy toDir="${output.dir}/myapp/WEB-INF/lib">
<fileset file="${build.classpath}" />
</copy>
It treats the whole classpath as one file. How do I get this to work?
The Ant Manual on the copy task contains the answer for your problem. One of the example snippets it provides:
Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.
<copy todir="dest" flatten="true">
<path>
<pathelement path="${java.class.path}"/>
</path>
</copy>
I think somethink like this should work:
<copy todir="${output.dir}/myapp/WEB-INF/lib" verbose="yes" flatten="yes" failonerror="no">
<fileset dir="${build.classpath}">
<include name="*.jar" />
</fileset>
</copy>
or with wildcard in include: <include name="**/*.jar" />
I think you should put all your colon separated jar files to one root folder. If it is not possible then create a separate task that put those jar files into one folder(may be temporary). And assign ${build.classpath} to that folder. Use <fileset dir="${build.classpath}"/> in your copy clause.
I hope, it should help.

Categories

Resources