Configure Sonar for Delphi with sub-modules - delphi

I can get Delphi analysis working on single projects, so the plugin works.
I can get Submodules analysis set up as per my previous question but no delphi analysis is performed.
refer to Configuration of build.xml file for Sonar modules with Ant for reference.
If I include the (I believe) required delphi configuration in the master build file it only works if I include the sonar.sources line.
BUT, when I include that line it is attempting to analyse all delphi code and is ignorning the submodules.
How (assuming it can be done) do I configure this to work or do I have to run each project completely separately?
Master Build File
<?xml version="1.0" encoding="UTF-8"?>
<project name = "EXO" default = "sonar" basedir = "." xmlns:sonar="antlib:org.sonar.ant">
<echo>Root Project</echo>
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
<target name="sonar">
<sonar:sonar key="EXO.key" version="0.1">
<property key="sonar.sources" value="." />
<property key="sonar.language" value="delph" />
</sonar:sonar>
</target>
</project>
Sub Project Build File
<?xml version="1.0" encoding="UTF-8"?>
<project name="CRM" default="all" basedir=".">
<echo>CRM Module</echo>
<property name="sonar.language" value="delph" />
<property name="sonar.projectKey" value="EXO:CRM" />
<property name="sonar.sources" value="." />
<target name="all" />
</project>

Related

JSP is not getting copied while creating war using Ant

I am using following Ant script to create a war of simple web application.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="war">
<path id="compile.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="compile">
<javac destdir="WebContent/WEB-INF/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath" />
</javac>
</target>
<target name="war" depends="compile">
<war destfile="build/myproject.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent">
<include name="**/*.jsp" />
</fileset>
<lib dir="WebContent/WEB-INF/lib" />
<classes dir="WebContent/WEB-INF/classes" />
</war>
</target>
</project>
It's creating the war but when I am opening the war, it's not containing JSP files due to which application is not running. Any idea what is wrong?
Also, right now I am coping war manually in Weblogic. Is there any Ant command which can deploy war?
I don't know exact answer but here is my way of using Ant build.xml for webapps. Give it a try. This works inside Eclipse or run from the command line. Few key points are:
build.xml has reference to compile-time libraries, including servlet-api.jar
dynamic META-INF/MANIFEST.MF
separate targets for compile, jar and war tasks to allow easier per project custom rules
webapp war don't have individual .class files but compiled web-inf/lib/mywebapp.jar library to minimize filesystem noice
you may create web/WEB-INF/classes/ folder and put some .properties file or extreme case "binary provided" class files. They are put inside war package along with other jsp,html,js files.
folder structure is very streamlined, I can use mywebapp/web/ folder directly in Tomcat service during development. Each html, jsp etc changes are reflected at runtime. Compiling jar triggers Tomcat to reload webapp instance.
Use this common folder structure for webapp project.
/mywebapp/ant.bat
/mywebapp/build.xml
/mywebapp/classes/
/mywebapp/src/
/mywebapp/src/META-INF/MANIFEST.MF
/mywebapp/lib/
/mywebapp/web/
/mywebapp/web/WEB-INF/web.xml
/mywebapp/web/WEB-INF/lib/
/mywebapp/web/META-INF/context.xml
mywebapp/build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="mywebapp" default="build" basedir=".">
<property name="name" value="${ant.project.name}" />
<property name="classes" value="./classes" />
<property name="src" value="./src" />
<property name="webdir" value="./web" />
<property name="version" value="1.0"/>
<property environment="env"/>
<path id="libs">
<pathelement location="lib/servlet-api.jar" />
<pathelement location="web/WEB-INF/lib/somelib1.jar" />
<pathelement location="web/WEB-INF/lib/somelib2.jar" />
<pathelement location="web/WEB-INF/lib/gson-2.2.4.jar" />
</path>
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<target name="updatemanifest" description="Update manifest">
<buildnumber file="build.num"/>
<copy file="${src}/META-INF/MANIFEST.MF"
todir="${classes}/META-INF/" overwrite="true" preservelastmodified="true"
/>
<manifest file="${classes}/META-INF/MANIFEST.MF" mode="update">
<attribute name="Implementation-Version" value="${version}.${build.number} (${TODAY})" />
<attribute name="Implementation-Title" value="${name}" />
</manifest>
</target>
<target name="clean" description="Clean compiled classes">
<delete dir="${classes}" />
</target>
<target name="compile" depends="clean" description="Compile classes">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" target="1.6" source="1.6" encoding="ISO-8859-1"
debug="true" debuglevel="lines,source"
excludes="" includeantruntime="false" >
<classpath refid="libs" />
<compilerarg value="-Xlint:deprecation" />
</javac>
</target>
<target name="jar" depends="updatemanifest" description="Create a .jar file">
<echo message="Build release: ${release}" />
<jar
manifest="${classes}/META-INF/MANIFEST.MF"
jarfile="${webdir}/WEB-INF/lib/${name}.jar" >
<fileset dir="${classes}">
</fileset>
</jar>
</target>
<target name="war" depends="compile,jar" description="Create a .war file">
<delete file="${name}.war" />
<zip destfile="${name}.war"
basedir="${webdir}"
excludes="
**/CVS*
"
/>
</target>
<target name="build" depends="war" description="Build lib">
</target>
</project>
src/META-INF/MANIFEST.MF
Implementation-Title: myappname
Implementation-Version: 1.0.0 (2010-03-01)
Implementation-Vendor: My Name Ltd.
Implementation-URL: http://www.myname.com
mywebapp/build.bat
call c:\apache-ant-1.7.0\bin\ant.bat build
pause
Build script creates war package and manifest.mf within web-inf/lib/mywebapp.jar is updated to have build number, title and version. Very handy you can use folder content as a template for new webapp projects. Just edit build.xml to have new project name.
Some compile-time dependencies point mywebapp/web-inf/lib folder. Non war-packaged libraries are put to mywebapp/lib/ folder for compile time only. I like keeping each dependency within project version control so thats a reason for this lib folder. You may use *.jar wildcard ant syntax but I explictly list each file for self documentation purpose.
Here is a bonus file to be used in Tomcat during development time. It publishes webapp on Tomcat and any changes in project folder is seen immediately, its very handy for client file changes (html,js,jsp).
this file is a copypaste from mywebapp/web/META-INF/context.xml file but an explicit docBase attribute is added.
It directs Tomcat to use files directly from project folder, no redeployment needed at runtime
Start tomcat and keep it running, you may run several webapp projects withing same Tomcat instance. Sometimes bigger development projects need it.
Remote debugging hook requires some java magic not included here
tomcat/conf/Catalina/localhost/mywebapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/mywebapp/web"
debug="0" reloadable="true" crossContext="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127.0.0.1" />
-->
<!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->
<!-- pooled db connection -->
<Resource name="jdbc/mywebappDB" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="2" maxWait="20000"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
username="myuserid" password="mypwd"
url="jdbc:sqlserver://mysqlserv1.com:1433;DatabaseName=MyDB;applicationName=mywebapp"
validationQuery="SELECT 1"
/>
<!-- <ResourceLink name="jdbc/mywebappDB" global="jdbc/mywebappDB" type="javax.sql.DataSource" /> -->
<Resource name="jdbc/mywebappDB2" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="20" maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
username="myuserid" password="mypwd"
url="jdbc:mysql://localhost:3306/myDB2?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="300"
/>
</Context>
ps: Ant build system is fine no matter what some people may say. Go with it as you please.

Configuration of build.xml file for Sonar modules with Ant

I am setting up a Sonar project (using the delphi plugin), for simplicity sake assume there are two modules I want to report on.
Each module is in it's own sub-folder and each has it's own build.xml file.
At this point I can successfully run the sonar tasks and generate reports for each module as an independent project.
My problem is with configuring the "master" build.xml file.
The module build.xml file looks like this:
<?xml version="1.0"?>
<project name = "CRM" default = "sonar" basedir = ".">
<!-- Add the Sonar task -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<target name="sonar">
<property name="sonar.projectKey" value="EXO:CRM" />
<property name="sonar.host.url" value="http://localhost:9000" />
<sonar:sonar workDir="." key="CRM.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
<property key="sonar.sources" value="." /> <!-- project sources directories (required) -->
<property key="sonar.language" value="delph" /> <!-- project language -->
<property key="sonar.delphi.codecoverage.excluded" value=".\tests" /> <!-- code coverage excluded directories -->
<property key="sonar.importSources" value="true" /> <!-- should we show sources or not? -->
<property key="sonar.delphi.sources.excluded" value="" /> <!-- excluded directories -->
<property key="sonar.delphi.sources.include" value=".\includes" /> <!-- include directories, "," separated -->
<property key="sonar.delphi.sources.include.extend" value="true" /> <!-- should we extend includes in files? -->
</sonar:sonar>
</target>
</project>
The "Master" build.xml looks like this:
<?xml version="1.0"?>
<project name = "EXO" default = "sonar" basedir = ".">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<target name="sonar">
<property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
<sonar:sonar workDir="." key="EXO.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
<!-- project sources directories (required) -->
<property key="sonar.sources" value="." />
<property key="sonar.language" value="delph" />
<property key="sonar.importSources" value="true" />
<property key="sonar.delphi.sources.excluded" value="" />
<property key="sonar.delphi.sources.include" value=".\includes" />
<property key="sonar.delphi.sources.include.extend" value="true" />
</sonar:sonar>
</target>
</project>
It is always scanning all sources (required value) i.e. it is not respecting my modules.
The only way I can get this to work currently is by limiting the source code like this
<property key="sonar.sources" value="./crm/,./exonet6000/" />
I'm sure I must be mis-configuring something obvious here.
EDIT: I have now what I believe is a more consistent set of files based on examples here https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/java-ant-modules
Master build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name = "EXO" default = "sonar" basedir = "." xmlns:sonar="antlib:org.sonar.ant">
<echo>Root Project</echo>
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
<target name="sonar">
<sonar:sonar key="EXO.key" version="0.1">
</sonar:sonar>
</target>
</project>
and one of the submodule files
<?xml version="1.0" encoding="UTF-8"?>
<project name="CRM" default="all" basedir=".">
<echo>CRM Module</echo>
<property name="sonar.language" value="delph" />
<property name="sonar.projectKey" value="EXO:CRM" />
<property name="sonar.sources" value="." />
<target name="all" />
</project>
At this point the sonar process is completing successfully BUT no actual anlysis is being done. A key point is that I am not seeing the echo of the submodule so I suspect these build tasks are not actually running.
If you look at this sample project using Ant and multimodules, I'd say that you should not specify any property inside the tag in your master build.xml file, and let the submodules specify those properties.
I've got a project with many submodules. My sonar target looks like this:
<target name="sonar" depends="build.dependencies" description="collect code metrics">
<property name="m1" value="a/build.xml,b/build.xml,c/build.xml,d/build.xml"/>
... more properties defining modules...
<property name="sonar.modules" value="${m1},${m2},${m3},${m4},${m5},${m6}..."/>
<property name="sonar.projectName" value="MyProject"/>
<sonar:sonar key="my:project" version="${version}" xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>
</target>
No property definitions in the sonar:sonar task at all, and it is working as I want it to.
Sources are defined in the submodule build.xml files. (Actually, in a base-build.xml that all our build.xml files include... but that's an Ant thing not directly related to Sonar.)

sonar ant build.xml file for running default Sun checks on Java project

I am a beginner to SONAR , i just need a help for a sample ant build file for running my java project name 'Hello World' with SONAR 's default Sun checks Quality profile .I have not found anywhere any proper ant guide for sonar. I am using SONAR 2.10 .
Please help me in starting with SONAR .
<project name="Example" default="Sonar" basedir=".">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\Program Files\Apache Software Foundation\ant\lib\sonar-ant-task-1.0.jar" />
</taskdef>
<!-- Out-of-the-box those parameters are optional -->
<property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3309/sonar" />
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<property name="sonar.jdbc.username" value="root" />
<property name="sonar.jdbc.password" value="root" />
<!-- Additional Sonar configuration (PMD need 1.5 when using annotations)-->
<property name="sonar.java.source" value="1.5"/>
<property name="sonar.java.target" value="1.5"/>
<property name="sonar.projectName" value="Example"/>
<property name="sonar.binaries" value="C:\Documents and Settings\tausif\Feature2\Example\bin"/>
<!-- SERVER ON A REMOTE HOST -->
<property name="sonar.host.url" value="http://localhost:8080/sonar" />
<target name="Sonar">
<!-- The workDir directory is used by Sonar to store temporary files -->
<sonar:sonar workDir="C:\Documents and Settings\tausif\Feature2\Sonar" key="com.example:example" xmlns:sonar="antlib:org.sonar.ant" >
<!-- source directories (required) -->
<sources>
<path location="C:\Documents and Settings\tausif\Feature2\Example" />
</sources>
</sonar:sonar>
</target>
</project>
The above two answers were realy helpful for me to create this xml file .
This is my sample build.xml . Can you please check what i am missing in it?
I have made Sun checks as default.My project name is Example.
You might find this (Sonar 2.6: Adds Continuous Inspection Support for Ant Community) or this (Analyse with Ant Task 1.0) documentation helpful.
You can refer below ant script which is specific to the sonar.
You can add it in your build.xml.
Below is the script with the details
<!-- Here you need to set the path which contains sonar specific jars required for ant e.g. path which contains sonar-ant-task-2.1.jar -->
<path id="sonar.classpath">
<fileset dir="${basedir}/sonar" includes="**/*.jar" />
</path>
<!-- This taskdef represents your ant lib for sonar you have to specify jar location along with jar name in class path no need to change the uri and resource-->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${basedir}\sonar\sonar-ant-task-2.1.jar" />
</taskdef>
<!-- This is the target we use to run sonar "depends" property is optional -->
<target name="sonar" depends="clean, compile">
<!-- specify your build version -->
<property name="build.version" value="0.0.0.1-Sonar"/>
<!-- specify your organization name its optional -->
<property name="mysonar.organizationName" value="XYZ"/>
<!-- specify your project Name -->
<property name="sonar.projectName" value="${project.name}" />
<!-- database url which is used by the sonar -->
<property name="sonar.jdbc.url" value="jdbc:mysql://<IP>:<Port>/sonar?useUnicode=true&characterEncoding=utf8" />
<!-- Driver name-->
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<!-- database user name -->
<property name="sonar.jdbc.username" value="test" />
<!-- database password -->
<property name="sonar.jdbc.password" value="test" />
<!-- url on which sonar is running-->
<property name="sonar.host.url" value="http://<IP>:<Port>" />
<!-- project key -->
<property name="sonar.projectKey" value="${mysonar.organizationName}:${sonar.projectName}" />
<!-- project version-->
<property name="sonar.projectVersion" value="1.0" />
<!-- location source files -->
<property name="sonar.sources" value="${src.home}/main/java" />
<!-- location of binaries after compilation-->
<property name="sonar.binaries" value="${basedir}/output"/>
<!-- location of sonar library-->
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>
</target>
Note: Make sure that location you specify are correct you can give absolute path as well.

ant large project. How to enforce common output directory structure - include,import and inhertAll=false?

I have a toplevel ant project and many subprojects under it.
./build.xml
./datamodel_src/src/build.xml
./datamodel_src/src/module1/build.xml
./datamodel_src/src/module2/build.xml
./infrastructure_src/src/build.xml
./interfaces_src/src/build.xml
Each of the subproject, I want to enforce a common output directory structure. Project will have a work area and each sub project will have its own work area under it. Each subproject should create its artifacts (lib, docs, classes etc) under a work area for the subproject.
So the output would be some thing like
c:/sandbox/mainprojectworkarea/subprojectworkarea/lib
c:/sandbox/mainprojectworkarea/subprojectworkarea/docs
c:/sandbox/mainprojectworkarea/subprojectworkarea/classes
Currently I do this as follows.
The toplevel build.xml is like below
<project name="toplevelproject" default="compile" basedir=".">
<target name="compile">
<ant dir="infrastructure_src/src" />
<ant dir="interfaces_src/src " /> <!--does not work-->
<ant dir="datamodel_src/src inhertAll=false" /> <!--works-->
</target>
</project>
common.xml is like below
<property environment="env" />
<property name="project.sandbox" value="${env.BUILD_HOME}/sandbox" />
<property name="sandbox" value="${project.sandbox}" />
<property name="pwa" value="${sandbox}/pwa" />
<property name="wa" value="${pwa}/${ant.project.name}" />
<property name="build" value="${wa}/build" />
<property name="lib" value="${wa}/lib" />
<property name="docs" value="${wa}/docs" />
<property name="exports" value="${wa}/exports" />
This is "included" into all projects. For example "datamodel_src/src/build.xml" is like below
<!DOCTYPE project [
<!ENTITY common SYSTEM "../../common.xml">
]>
<project name="dmodel" default="compile" basedir=".">
&common;
<target name="compile">
<echo message="will create lib in ${lib}"/>
<echo message="will create docs in ${docs}"/>
<ant dir="module1" inheritAll="false"/> <!--works fine-->
<ant dir="module2" /> <!--does not work -->
</target>
</project>
This works when I set inhertiAll=false for ant calls.
Is there a better and correct way to?
Expanding answer from Kevin to this question.
Using import the common.xml becomes a real project like below
<project name="toplevelproject" default="compile" basedir=".">
<property name="toplevel" value="settotrue"/>
<target name="compile">
<ant dir="infrastructure_src/src" />
<ant dir="interfaces_src/src" />
<ant dir="datamodel_src/src" />
</target>
</project>
The "datamodel_src/src/build.xml" is now some think like below.
<project name="dmodel" default="compile" basedir=".">
<import file="../../common.xml" />
<target name="compile">
<echo message="will create classes in ${build}"/>
<echo message="will create lib in ${lib}"/>
<ant dir="module1" inheritAll="false"/> <!--works fine-->
<ant dir="module2" /> <!--does not work -->
</target>
</project>
The import gives option to have common targets etc, hence I would go with it.
I'm doing something similar using imports rather than includes. All my common targets and properties are defined in a common build file and each subproject just imports the common file. When you import a file, the properties defined in that file become relative to the importing file.
So I would try doing the following:
Move your compile target from your subproject build files into your common.xml.
Import your common.xml into each subproject build.xml.

sorting as well as removing duplicacy

<?xml version="1.0"?>
<project name="sortlist11" default="sortlist11">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<property name="my.list" value="z,y,x,w,v,u,t" />
<property name="my.list1" `value="5,3,6,1,8,4,6" `/>
<target name="sortlist11">
<sortlist property="my.sorted.list" value="${my.list}" delimiter="," />
<sortlist property="my.sorted.list1" value="${my.list1}" delimiter="," />
<echo message="${my.sorted.list}" />
<echo message="${my.sorted.list1}" />
</target>
</project>
here second echo print 1,3,4,5,6,6,8 but how can i remove redundancy?
Every language running in JVM via Bean Scripting Framework may be used in ant with full access to the ant api. Here's a solution with Groovy for your problem =
<project>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<property name="my.list" value="z,y,x,w,v,u,t"/>
<property name="my.list1" value="5,3,6,1,8,4,6"/>
<groovy>
properties.'my.sorted.list' = properties.'my.list'.split(',').sort().toString()
properties.'my.sorted.list1' = properties.'my.list1'.split(',').toList().unique().sort().toString()
</groovy>
<echo>
$${my.sorted.list} => ${my.sorted.list}
$${my.sorted.list1} => ${my.sorted.list1}
</echo>
</project>

Resources