Add compilation symbol for project referenced in Visual Studio - visual-studio

I have my project containing most of my functions (MainProject).
I want to have a web service referencing MainProject and use some of those functions.
However I want to add conditionnal compilation in MainProject to not include all the functions and only build them if a webservice project is needing them.
I added one function like this:
#if WEBSERVICE
public void MyApi()
{
}
#endif
And in the project settings of my web service I tried adding a define constants:
<ProjectReference Include="..\..\api\MyAPIs.csproj">
<Project>{ed6ba855-7082-4b28-99a2-3e9fcf810433}</Project>
<Name>API</Name>
<DefineConstants>WEBSERVICE</DefineConstants>
</ProjectReference>
My goal is to add the force the WEBSERVICE symbol to be added when building my web service project.
This doesn't work, when I check the build output my symbol is not added when building the dll for the project.
I know I could add a build configuration to solve that issue but since I will have several webservice projects I would rather keep my build configurations small and just add an extra symbol when needed.
I have also added my WEBSERVICE symbol in each configuration of my Webservice Project
<DefineConstants>$(DefineConstants);SERVICE_BOT</DefineConstants>
Is what I'm trying to achieve possible?

Related

Creating dll and lib from one project in the solution

dll project for which i want to create unit tests.
In order to run unit tests i need my test project to compile all dependencies and i don't want to add my cpp files to test project.
The solution is to add lib in references but i have no clue how should i compile my project as both dll and as lib. Is this even possible ? I suggest that could be easy with naked cmake/make.
What i could do is create another project with same files and build it as a .lib. I think this solution is very primitive and it will require me to add .cpp files to projects twice if i ever want to add something new. I would prefer a solution in which i could have only one project in solution and have it build .dll (a main component) and a .lib for UT project reference.
I know i could also make UT load my .dll and have that kind of dynamic linkage, but that would make it harder due the need of creation of dll wrapper to access those functions.
I also considered using batch build and have custom build configuration. But this will require me to batch build every time i want to run UTs. I'd rather have it chose automaticaly. Maybe if i set UT project to be build under custom configuration the dependency will also be build with custom config?
Does Visual Studio 2015 bring any simple solution to this problem?

MSVC C2859 when using a different build configuration of another project as a library

I have a fairly large multi-project C++ solution in Visual Studio 2015. Some of the projects compile to static libraries which are used by other projects, and most of them use precompiled headers to speed up compilation. Each project also has multiple build configurations: debug, release, and several testing configurations which always build an executable to run the tests (even if the normal configurations build a static library).
When building debug and release configurations, or when doing a full rebuild, everything works well, but when doing an incremental build of a test configuration for a project that uses another project's static library, I get C2859 errors which cause the build to fail.
For example, let's say I have a project peach which builds a static library, and a project cobbler that relies on peach. The precompiled header for cobbler references only system and external libraries (no headers from inside the solution). cobbler's test configuration references peach.lib. peach.lib is created by peach's release configuration, so I have a solution configuration called cobbler-test which specifies that:
peach uses its release project configuration
cobbler uses its test project configuration.
Building cobbler-test from scratch (or rebuilding it, clean & build, etc) works fine. But if I then modify a source file called crust.cpp in cobbler and try to build, I get this error:
c:\...\cobbler\src\crust.cpp(1): error C2859: C:\...\out\cobbler-test.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.
Again, this only happens when referencing a static library from the same solution that was built with a project configuration name different from the current one. With both projects using release or debug, incremental builds work fine.
Having to do a full rebuild every time defeats the purpose of using precompiled headers in the first place. Is there any way to get incremental testing configurations to work without having to resort to creating extra project configurations for every combination of projects?
My current solution to this problem is not to use testing configurations, and to create separate projects for testing projects that generate static libraries. This allows all dependent projects to use the same project configuration and allows the precompiled headers to work their magic without blowing up when doing incremental builds.
While this works fairly well for the static libraries, since they can easily be imported with #pragma comment(lib, ...), it's a bit more problematic for projects that build standalone executables. Thankfully, in my case most of those projects don't have a lot of stuff that needs testing.

MSBuild conditional reference in ProjectC based on whether running ProjectA or ProjectB

I have three projects in my solution:
ProjectA : contains reference to ProjectC
ProjectB : contains reference to ProjectC
ProjectC : references third.party.dll.A or third.party.dll.B
During compilation, I want Project C to reference either third.party.dll.A or third.party.dll.B depending on which one of ProjectA or ProjectB that I a have set as the startup project and am launching/compiling via the IDE.
How can I set up my solution so I can use an MSBuild conditional reference in ProjectC.csproj to determine whether ProjectA or ProjectB is currently being compiled?
(In the .csproj file I tried checking if $(DefineConstants) contains a certain value, but it seems like $(DefineConstants) only contains the symbols defined in ProjectC, and not those defined in ProjectA and ProjectB.)
(FYI: I am using Xamarin Studio on a Mac. But I assume that if there is a way to make this work for Visual Studio, it should also work in Xamarin Studio.)
No, you cannot do that. Project references in MSBuild are not configurable to the extend you want them to be, configuration of the build for referenced projects are hard-coded in standard msbuild .targets files.
There are couple of possible workarounds for your case. The easiest one is to create two projects: ProjectC1 and ProjectC2. C1 will use one of the third-party library, while C2 will use another one. You can make ProjectC1 and ProjectC2 share the same source files, so there will be no code duplication.
Another possible workaround is to make references to third.party.dll.A and third.party.dll.B with conditional assignment, like this:
<ItemGroup>
<ProjectReference Condition="'$(BuildA)' == 'true'" Include="pathto\third.party.dll.A">
<Name>third.party.dll.A</Name>
</ProjectReference>
<ProjectReference Condition="'$(BuildB)' == 'true'" Include="pathto\third.party.dll.B">
<Name>third.party.dll.B</Name>
</ProjectReference>
</ItemGroup>
If you do this, you should also set similar conditions for the build output directories, based on values of properties BuildA and BuildB you should configure $(IntDir) and $(OutDir) to separate locations, because your incremental build will be seriously broken otherwise.
Then you can build from command line passing additional parameters:
> msbuild ProjectA.csproj /p:BuildA=true
or
> msbuild ProjectB.csproj /p:BuildB=true
To make the build work like this in Visual Studio, or Xamarin Studio, you have to set environment variable before launching the VS or XS. Note that you cannot switch from one configuration to another without re-launching VS or XS.

Java web application modularize with spring

I'm trying to build a project structure like this:
Project
|--Web_module.war
|--Data_module.jar(Spring)
|--Util_module.jar
|--other public api...
which means, different modules should be packed into different jars, so i have to have more spring configurations(application-context.xml) for different modules (e.g. for data module and for web module).
My question, how could I organize all the configuration files to include them correctly in the web module.
Thanks in advance.
Plan to have a single eclipse project for each jar file that you anticipate.
Choose the jars files / eclipse projects as per your project functionality to be modular and self contained, as far as possible.
Use junit tests in each eclipse project to thoroughly test individual projects/modules, using spring unit test support
Each eclipse project will contain its own spring config context file eg Util_module project might contain a util-context.xml
Finally have an eclipse dynamic web project as a wrapper web application which will aggregate all your "module" projects
UI artifacts like HTML, JS, JSPs, etc plus java code which uses web application contexts like controllers, servlet filters etc should be included in the eclipse web project
In the eclipse web project's java build path, but the module "projects" as "required" projects
In the eclipse web project's deployment assembly, add module "projects" as dependencies.
now when you build-all and deploy the web app, all depending module projects will compile and deploy as well, but more importantly, all project functionality will be divided into seperate modular projects
setup dependencies between projects with care, so as not to introduce cyclic dependencies
dont be afraid to refactor project structure when needed to maintain clean and relevant modules
For your modules to publish their own configuration (and your main application to detect them automatically), you can, in your main applicationContext.xml, import other context.xml files from the classpath using a pattern with wildcards :
<import resource="classpath*:conf/moduleContext.xml" />
This tells spring to find and read files in all jars that match conf/moduleContext.xml.
Note there is a little limitation to this : you must have your context files in at least one directory (not in the root of the classpath). This is why in my example you have de "conf" directory.

Reference External Project in Spring Configuration

I am trying to reference a project in my main project. I am a .NET developer so I am having a hard time knowing the correct way to reference it. I have a data project and a data project in a MAVEN multiple project setup. In the screenshot you can see I am trying to reference a DAO object from my data project in the security.xml file in my Spring MVC web project. When I run the project it says it can not find the correct class.
Actually I fixed this by adding a Maven dependency of my project, which created a JAR for Tomcat, and fixed the issue. I didn't know you could search the Maven Local Repository to use your own projects.

Resources