Add a copyright notice to all files in a directory using Ant
I want to add a copyright notice to a series of files generated in an Ant build. What's the easiest way to do it?
I thought about using <concat>
but I don't understand how to run it on a fileset without concatenating all the files in th开发者_StackOverflowe fileset.
Thanks!
Looks like <concatfilter>
may be your friend.
<echo message="/* Copyright text */${line.separator}" file="copyright.txt"/>
<copy todir="dest_dir">
<fileset dir="src_dir"/>
<filterchain>
<concatfilter prepend="copyright.txt"/>
</filterchain>
</copy>
If you only want to selectively add the copyright then you can use a copy command that replaces @COPYRIGHT@ tokens within the files.
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="COPYRIGHT" value="This text is my copyright!"/>
</filterset>
</copy>
One option that wasn't mentioned so far was writing a custom Ant Task in Java. I realized there are many more factors to consider than just pre-pending a license to a file after starting my own work on a custom task which has more requirements than 'concat' can handle. Some of the scenarios and rules I had to consider:
- Every file that is going to be distributed should contain 1 and only 1 copy of the license.
- Does the current file have a license?
- Does the current file have the current license?
- If the file starts with a comment, is it a license, and it is current?
- Is this a file that requires the license? (Don't want to write text into a binary file, or a file that won't be distributed.)
It's still a work in progress, so I don't necessarily have any code to share, but maybe the idea can help point you in the right direction if your requirements are more than out-of-the-box ant can handle, and you don't want to use antcontrib.
If you are using or considering maven, then now is the time to make the switch. You can drive ant builds from a pom.xml as well as doing any file pre-processing before building. Check out the mycila plugin which works right out of the box.
Writing your own ant task
Maven in 5 minutes
Link to Mycila Maven plugin
One option might be to use <concat>
but within an antcontrib <foreach>
task.
精彩评论