I am trying to display the value of a property of my pom.xml file on an HTML page, on the client side (so that an admin can see it on the administration page) but I can't find a way to do it.
I have tried with PHP and JS and so on but nothing is working... Here's my last attempt:
function populatePre(url) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
document.getElementById('contents').textContent = this.responseText;
};
xhr.open('GET', url);
xhr.send();
}
<p id="testButton"><button type="button" onclick=populatePre('C:\path\to\my\pom.xml')>Click Me!</button></p>
<pre id="contents"></pre>
Basically, the HTML page is in a folder and the pom.xml file is in another one (../../path/to/pom.xml). I don't know how to access it nor how to read it...
</div>
I found a way by doing this and then sending the value to the view. Here's the code (simplified):
public function index(Request $request)
{
$pom = '../../path/to/file/pom.xml';
$myFile = fopen($pom, "r") or die("Unable to open file!");
$fileContents = fread($myFile, filesize($pom));
$tempString = substr($fileContents, strpos($fileContents, 'valueToFind') + 49, 50);
$foundValue = substr($tempString, 0, strpos($tempString, '<')); fclose($myFile);
return view('administration.index', ['foundValue' => $foundValue]);
}
The problem is here : C:\path\to\my\pom.xml
, it's a path to your computer, you need to use a server. Basically, you url must be like localhost:8080/path/to/my/pom.xml
or your-site.com/path/to/my/pom.xml
. So use an Apache server if you already use PHP.
It's possible to describe HTML page as filtered resource in pom.xml. Something like
<resource>
<directory>path/to/html_directory</directory>
<includes>
<include>your.html</include>
</includes>
<filtering>true</filtering>
</resource>
Then put placeholder into HTML as ${yourPropertyName}
and it will be substituted with property value during build process. For details see Maven Resources Plugin filtering option description. Will this solve your problem?