How to get the Path of an InputFile in a Sensor? version 6.7

46 views
Skip to first unread message

xti...@gmail.com

unread,
Jan 12, 2018, 11:41:38 AM1/12/18
to SonarQube
Hello fellows,

I am migrating a existing plugin that works for Sonarqube 5.6 to 6.7.1.

in order to handle the files  in a Sensor I used:

for (InputFile file : context.fileSystem()
                .inputFiles(context.fileSystem().predicates().hasLanguage("cs"))) {            
            Path fileInSonar = Paths.get(file.file().getPath());
            // code that process the file....where the magic happens
        }

I just found that the method file(), as well as the absolutePath() and relativePath() methods has been deprecated.

what is the option to have the path of the file?

Julien HENRY

unread,
Jan 15, 2018, 3:00:11 AM1/15/18
to SonarQube
Hi,

Those methods were deprecated in an attempt to encourage Sensors to not rely on local filesystem to read file content, but instead use provided content() or inputStream() methods.

Can you share why you need to get the Path of the file?

++

Julien

xti...@gmail.com

unread,
Jan 15, 2018, 9:07:17 AM1/15/18
to SonarQube

Hi Julien,

As you pointed the methods content() or the inputStream() will fit if you need to read file content, but that is not my intention.

I need the path (relative or absolute) because I am using an external tool to generate some metrics, that tool creates an XML report, where all the files analyzed are included and distinguished by its path; As I have several files with the same name but in different folders I need to filter by its path, take the information regarding that file, parse it and save it into Sonarqube.

thanks.

Xtian

Julien HENRY

unread,
Jan 15, 2018, 11:33:41 AM1/15/18
to xti...@gmail.com, SonarQube
Hi Xtian,

The correct way would be to use InputFile::uri() to build a Path: Paths.get(inputFile.uri()). On scanner side you are guaranteed this uri will always be a file:// (which is not true on SonarLint for example).

++

Julien Henry | SonarSource

Developer

https://sonarsource.com


--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/-qmKmOo3B2g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/edb3e400-833f-41eb-9faf-5b527956482f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

xti...@gmail.com

unread,
Jan 17, 2018, 4:57:30 PM1/17/18
to SonarQube
Thanks a lot!!!

Reply all
Reply to author
Forward
0 new messages