Extract folders in gradle and create a jar - plugins

I have this structure in a zip file
classes
|--com: A.class, B.class
|--org: C.class, D.class
|--android: X.class
|--stuff: Stuff.inf, info.txt
I want be able to extract only the folders with .class files: com, org, android. And put them on a jar.
So far I have done this:
task createJar {
//unzip the file
FileTree zip = zipTree('runtime.jar')
FileTree zip2 = zip.matching {
include 'classes/**/*.class'
}
zip2.each { file -> println "doing something with $file" }
//create the jar
jar{
from zip2
destinationDir file('GradleTests/testResult')
}
}
But I get the jar with the classes folder on it, like: classes/org/apache/http/entity/mime/content/StringBody.class
And i want it like: org/apache/http/entity/mime/content/StringBody.class
Any idea how?
Thanks!

So I think there are two main things that can get you closer to what you want:
Use the zipTree directly in the jar task rather than creating an intermediate zip.
Use eachFile to change the relative path of each file to strip off the first folder.
jar {
from zipTree('runtime.jar') {
include 'classes/**/*.class'
eachFile { it.relativePath = it.relativePath.segments[1..-1] }
}
}
Jar tasks have all of the CopySpec methods, which provide the eachFile abilities.

Related

How to list all local mp3 files on flutter2(null safety)?

The code should be working on flutter2, with android and ios(if possible)
Searching for files
Do you want to simplify your life? Use this package:
import 'package:glob/glob.dart';
Stream<File> search(Directory dir) {
return Glob("**.mp3")
.list(root: dir.path)
.where((entity) => entity is File)
.cast<File>();
}
Do you want to avoid adding a new dependency in your project? Manipulate the Directory itself:
Stream<File> search(Directory dir) {
return dir
.list(recursive: true)
.where((entity) => entity is File && entity.path.endsWith(".mp3"))
.cast<File>();
}
Defining the search scope
You'll also need a Directory where you'll search for MP3 files.
Is it the root directory (i.e. search ALL the files in the device)? Use this answer.
Is it another directory? Pick one from this package.
Usage
final Directory dir = /* the directory you obtained in the last section */;
await search(dir).forEach((file) => print(file));

Creating filter using org.eclipse.core.resources.IProject class

I am creating a jar file using org.eclipse.core.resources.IProject. while creating i would like to exclude few files from the selected directory based on the file extension.
if (selectPackageCombo.getText().equals(item)) {
IProject project = availableProjects.get(i);
Package package = EclipsePackageRepository.instance().getPackage(project);
if (package != null) {
try {
project.createFilter(IResourceFilterDescription.EXCLUDE_ALL| IResourceFilterDescription.FOLDERS|IResourceFilterDescription.FILES,
new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter", "1.0-name-matches-false-false-Test"),IResource.BACKGROUND_REFRESH, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
jarData.setSelectedProject(project);
jarData.setOutputSuffix(package.getPackageResource().getType());
}
break;
}
Please help me how to create the object of FileInfoMatcherDescription to exclude all the files with the extension ".ayt"
There isn't much informaton about this but it looks like you can work out the values by using the 'Resource Filters' page in the project properties and creating the filter you want. The id and arguments values will then be saved in the .project file in the project which you can read.
So for your requirement I get
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-*.ayt</arguments>
</matcher>
so the constructor is
new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
"1.0-name-matches-false-false-*.ayt")

How can I get the project path in Scala?

I'm trying to read some files from my Scala project, and if I use: java.io.File(".").getCanonicalPath() I find that my current directory is far away from them (exactly where I have installed Scala Eclipse). So how can I change the current directory to the root of my project, or get the path to my project? I really don't want to have an absolute path to my input files.
val PATH = raw"E:\lang\scala\progfun\src\examples\"
def printFileContents(filename: String) {
try {
println("\n" + PATH + filename)
io.Source.fromFile(PATH + filename).getLines.foreach(println)
} catch {
case _:Throwable => println("filename " + filename + " not found")
}
}
val filenames = List("random.txt", "a.txt", "b.txt", "c.txt")
filenames foreach printFileContents
Add your files to src/main/resources/<packageName> where <packageName> is your class package.
Change the line val PATH = getClass.getResource("").getPath
new File(".").getCanonicalPath
will give you the base-path you need
Another workaround is to put the path you need in an user environmental variable, and call it with sys.env (returns exception if failure) or System.getenv (returns null if failure), for example val PATH = sys.env("ScalaProjectPath") but the problem is that if you move the project you have to update the variable, which I didn't want.

Gradle - Copying / Renaming file - making 0 file size / bytes for all files

I'm using Gradle build to compile Java.
During the build, I get successful build deliverables BUT I want to add some extra files which are just the copy or rename of some files which exist within the built ".war" file by Gradle build.
The following code is what I'm using. What I'm trying to do is:
1. unwar a .war file in a build/tmp/warApplication folder
2. copy/rename a file i.e. oldname-1.2.3 to newname-1.2.3
3. copy/rename oldname.war.properties file to newname.war.properties
4. copy/rename oldname.jar.properties file to newname.jar.properties
5. war all data inside build/tmp/warApplication folder and create original .war file (which is projectname.war)
Issue:
1. After unwar step is complete, I see under tmp/warApplication folder, all files in the tree have valid file size.
2. warApplication.doLast - works i.e. it doesn't give any error mesg.
3. Once all copy/rename steps are complete, I see the file size under tmp/warApplication folder tree or subtree level is now 0 bytes in size.
WHAT am I missing? How can I copy a file A of size X bytes to file B of same X bytes.
// add a few extra files
warApplication.doLast {
def tempWarDirU = new File("${buildDir}/tmp/warApplication")
tempWarDirU.mkdirs()
ant.unwar(src: "${buildDir}/thidsWar/${thidsProjectName}.war",
dest: "${buildDir}/tmp/warApplication")
println ""
println "--- Inside warApplication - last stage ---"
println ""
copy {
from "${buildDir}/tmp/warApplication"
into "${buildDir}/tmp/warApplication"
rename (/([a-z]+)-([0-9]+\.[0-9]+.+)/, 'newname-$2')
}
copy {
from "${buildDir}/tmp/warApplication/WEB-INF/classes"
into "${buildDir}/tmp/warApplication/WEB-INF/classes"
rename (/([a-z]+)\.war\.([a-z]+)/, 'newname.war.$2')
}
copy {
from "${buildDir}/tmp/warApplication/WEB-INF/classes"
into "${buildDir}/tmp/warApplication/WEB-INF/classes"
rename (/([a-z]+)\.jar\.([a-z]+)/, 'newname.jar.$2')
}
ant.jar ( update: "true", destfile: "${buildDir}/thidsWar/${thidsProjectName}_1.war") {
fileset(dir: "${buildDir}/tmp/warApplication",includes: '*/**')
}
println ""
println ""
}
An easier solution would be, thanks to Pete N.
task renABCToXYZ {
doLast {
file("ABC").renameTo(file("XYZ"))
}
}
Had to mention the "include" section. without that, it'was copying all files into 0 bytes in the tree.
copy {
from "${buildDir}/tmp/warApplication"
into "${buildDir}/tmp/warApplication"
include "${thidsProjectName}-$project.version"
rename (/([a-z]+)-([0-9]+\.[0-9]+.+)/, 'newname-$2')
}
copy {
from "${buildDir}/tmp/warApplication/WEB-INF/classes"
into "${buildDir}/tmp/warApplication/WEB-INF/classes"
include "${thidsProjectName}.war.properties"
rename (/([a-z]+)\.war\.([a-z]+)/, 'newname.war.$2')
}
copy {
from "${buildDir}/tmp/warApplication/WEB-INF/classes"
into "${buildDir}/tmp/warApplication/WEB-INF/classes"
include "${thidsProjectName}.jar.properties"
rename (/([a-z]+)\.jar\.([a-z]+)/, 'newname.jar.$2')
}

Cannot distinguish file from folder

I am trying to list all the files contained in a directory UROP by using stat(). However, the directory does not contain only files, but also folders, which I want to search too. Therefore, I am using recursion to access the folders whose files I want to be listed.
However, the if conditions in my loop do not manage to distinguish a file from a directory, and all files appear as directories; the result is an infinite recursion The code is the following. Thank you in advance!
using namespace std;
bool analysis(const char dirn[],ofstream& outfile)
{
cout<<"New analysis;"<<endl;
struct stat s;
struct dirent *drnt = NULL;
DIR *dir=NULL;
dir=opendir(dirn);
while(drnt = readdir(dir)){
stat(drnt->d_name,&s);
if(s.st_mode&S_IFDIR){
if(analysis(drnt->d_name,outfile))
{
cout<<"Entered directory;"<<endl;
}
}
if(s.st_mode&S_IFREG){
cout<<"entered condition;"<<endl;
cout<<drnt->d_name<<endl;
}
}
return 1;
}
Instead of if(s.st_mode&S_IFDIR) and if(s.st_mode&S_IFREG),
try if (S_ISDIR(s.st_mode)) and if (S_ISREG(s.st_mode)).