I have a nuget getting created and Content folder getting created in the nuget. I want my files to not be in Content folder but be in the Test folder in that nuget. (So, user won't see files by default.)
How Can I move files from ContentFolder to Test folder ?
My csproj has :
<ItemGroup>
<Protobuf Include="*.proto" GrpcServices="Both" />
<Content Include="#(Protobuf)" /> // If I remove this line - Content folder disappers, but I want to keep this data into a Test folder in my nuget
</ItemGroup>
You can use PackagePath to control where content is placed in NuGet packages.
<ItemGroup>
<Protobuf Include="*.proto" GrpcServices="Both" />
<Content Include="#(Protobuf)" PackagePath="Test" />
</ItemGroup>
Related
I am already using Directory.Build.props to add certain packages to all the C# projects like this:
<Project>
<ItemGroup>
<PackageReference
Include="StyleCop.Analyzers"
Version="1.2.0-beta.435"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'"
/>
</ItemGroup>
</Project>
Now I added a Test folder to the solution where I want to store all the tests, the folder tree look like this:
The folder it's a solution folder and appears in the sln file.
How can I create a condition to add xunit and Moq to only the csproj inside that folder?
Following approach only works if Test is a folder on the file system instead of a solution folder:
You can add another Directory.Build.Props into the Test folder and include the parent folder's Directory.Build.Props like this:
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<!-- Your content -->
</Project>
Since your question is about packages you could also consider Central Package Management as an alternative.
Okay,
I am really struggling by creating a nuget package for a BCL.
During the development, I have pushed created some *.scss files for my blazor components. Now, I want to use these components in a separate nuget package and consume it within other projects.
So far so good, but unfortunately all my css-files aren't within the nuget package.
What I've tried:
All my components has it's own *.scss file. After building the project, there are some css files, e.g. HelloWorld.razor.css.
When I am runing the dotnet pack command, there aren't any css files within the nuget package.
I've already included the EmbeddedResource Element to my csproj file, now I can see all the css file, but my BlazorApp won't apply the styles at all.
I've already studied the documentation (https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation) but I still not able to enable the styles.
I have also tryied to reference the project directly to my BlazorApp, and it worked. Is it a NuGet issue?
My csproj looks like follows:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyVersion>1.0.12.0</AssemblyVersion>
<FileVersion>1.0.12.0</FileVersion>
<Version>1.0.12-rc.33</Version>
</PropertyGroup>
<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>
<PropertyGroup>
<StaticWebAssetBasePath>_content/$(PackageId)</StaticWebAssetBasePath>
<DisableScopedCssBundling>false</DisableScopedCssBundling>
<ScopedCssEnabled>true</ScopedCssEnabled>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**\**\*.css" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Delegate.SassBuilder" Version="1.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.2" />
</ItemGroup>
</Project>
I'm wanting to provide some additional files in a nuget package so that whenever and wherever it is installed, the files are provided and in the correct location.
The problem I'm having is that I have code that needs to reference these files in order to run.
The files are 3 .exe files and a .jar file in a single directory...
This is the code that references that folder location...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\WebDrivers\\Drivers\\";
I want to be able to package this code and the files up so that whenever this nuget package is installed, this code will point to a valid location.
How do I do this?
First thing I tried was to add the following to the .csproj file
<ItemGroup>
<None Update="WebDrivers\Drivers\chromedriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\geckodriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\msedgedriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\selenium-server-standalone-3.141.0.jar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
This only works if you reference the project in the same solution. It doesn't work after you have done a dotnet pack and installed the package in a different project/solution.
Second thing I tried was the accepted answer here...
Copy files from Nuget package to output directory with MsBuild in .csproj and dotnet pack command
But I couldn't get any files to copy...
SASelenium.Framework.csproj
<ItemGroup Label="FilesToCopy">
<Content Include="SASelenium.Framework.targets" PackagePath="build/SASelenium.Framework.targets" />
<Content Include="LogFiles\*.config" Pack="true" PackagePath="contentFiles\LogFiles">
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
SASelenium.Framework.targets
<ItemGroup>
<LogFiles Include="$(MSBuildThisFileDirectory)\..\contentFiles\LogFiles\*.config" />
</ItemGroup>
<Target Name="CopyLogFiles" BeforeTargets="Build">
<Copy SourceFiles="#(LogFiles)" DestinationFolder="$(TargetDir)CopiedLogFiles\" />
</Target>
I don't see any files when I build the project where this package is installed.
Even if this did work, how would I ensure the code within the package is always pointing at these files when running from any project?
I solved this by updating my .csproj to the following for each required file...
<Content Include="WebDrivers\Drivers\chromedriver.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
<pack>true</pack>
</Content>
After a dotnet pack I was able to see the files by inspecting package
After adding the nuget package to a new project and building the solution I was able to see the files in the bin folder...
Which means the code to find the file folder based on the currently executing assembly works as hoped.
I have a separate database project that I’d like to build within the same solution and then reference the created dacpac. When I try adding the database project it builds fine and the dll is added to the secondary project file, but the dacpac is not.
Is there a way that I can have the dacpac copied into my main project through the msbuild? I keep on thinking that there should be a way to modify either the sqlproj file or csproj file so that the dacpac is included as one of the project outputs. My knowledge of msbuild is not extensive, I’ve not been able to figure it out.
It seems to me that I need to add the dacpac somehow to say the '#(ReferenceCopyLocalPaths)' item but I have not been able to figure it out. Any tips or suggestions would be appreciated.
I tried doing something a little like what is referenced here MSBuild - ItemGroup of all bin directories within subdirectories by doing:
<Target Name="AfterBuild">
<Message Text="#(MainAssembly)" />
<!--<DacPacs Include="%(ProjectReference.Directory)**" />-->
<ItemGroup>
<DacPacs Include="%(ProjectReference.Directory)**/*bin*/*.dac" />
</ItemGroup>
<Message Text="#(ReferenceCopyLocalPaths)" />
<Message Text="DacPacs: #(DacPacs)" />
<Message Text="Target Database: $(TargetDatabase)" />
</Target>
which gives nothing for DacPacs (when the wildcard is added). Also I tried referencing one of the item groups from the sqlproj file but it comes out empty to:
In the project properties, you can add a pre-build event command line to get a copy of the dacpac file.
Or you can just add it in the csproj :
<PropertyGroup>
<PreBuildEvent>copy "$(SolutionDir)DatabaseProject\bin\$(ConfigurationName)\DatabaseProject.dacpac" "$(ProjectDir)\DatabaseProject.dacpac"</PreBuildEvent>
</PropertyGroup>
This will only work if the database project was built first, so you should add a dependency. Right clic on the solution and select Project Dependencies..., then select the main project and check that it depends on the Database project.
Add this to your csproj:
<ItemGroup>
<Content Include="..\DatabaseProject\bin\Debug\DatabaseProject.dacpac">
<Link>DatabaseProject.dacpac</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
This solution worked for me with a .NET 6 project which references a .sqlproj project using Micrsoft.Sql.Sdk. Assume there are two projects: DacpacDepender (.NET 6 project) and Dacpac (SQL project).
In DacpacDepender.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- some properties redacted for brevity -->
<TargetFramework>net6.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<!-- Ensures build order, the DACPAC is _guaranteed_ to exist when included -->
<ProjectReference Include="..\Dacpac\Dacpac.sqlproj">
<!-- Companion to .sqlproj CopyBuildOutputToOutputDirectory=false -->
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<!-- Suppresses a warning about this project not being compatible with SQL project -->
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<!-- Use `$(Configuration)` to reference the DACPAC output location regardless of build configuration. -->
<Content Include="$(ProjectDir)..\Dacpac\bin\$(Configuration)\Dacpac.dacpac" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
In Dacpac.sqlproj:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build">
<Sdk Name="Microsoft.Build.Sql" Version="0.1.3-preview" />
<PropertyGroup>
<!-- some properties redacted for brevity -->
<!-- Prevents outputting the DLL (some may need it) -->
<CopyBuildOutputToOutputDirectory>false</CopyBuildOutputToOutputDirectory>
<CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
<Name>Dacpac</Name>
<NetCoreBuild>True</NetCoreBuild>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
</Project>
Please note that <RestorePackagesWithLockFile /> is not strictly necessary but ensures dotnet restore --locked-mode works, which is a nicety for reproducible builds.
I would like to change the output folder of the ajax minifier so that when i build my project, all javascripts end up in the output folder instead of a copy next to the original file.
<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
<Target Name="AfterBuild">
<ItemGroup>
<JS Include="SourceScripts\*.js" /> <!--Output="Scripts\*.js"-->
</ItemGroup>
<ItemGroup>
<CSS Include="**\*.css" Exclude="**\*.min.css" />
</ItemGroup>
<AjaxMin
JsSourceFiles="#(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js"
CssSourceFiles="#(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
</Target>
Something like this. I have one folder called SourceScripts and one mirrored folder called Scripts. When i build, i want the scripts in SourceScripts (and its folders) to be minified and copied to the Scripts folder (same folder structure). Is this possible?
We are using VS 2010.